473,801 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ByteArray to String


What is the best way to convert a bytearray into a string ?

I thought I could do it using the code below, but is there anything
faster/easier than this? Thanks!

Chris

int aLength = 100000;
byte[] myByteArray = new byte[aLength ];
string myString = "";
for (int i=0; i<aLength ; i++)
{
myString += (char) myByteArray[i];
}

//work with this new string: cut out every char=='A'
myString.split( 'A');

....

Nov 17 '05 #1
5 2239
I've been doing this is VC# Express 2005 Beta:

ASCIIEncoding AE = new ASCIIEncoding() ;
string TextString = AE.GetString(by te-array);

Don't know if it works in other versions, so you'll need to check in your
specific VS release.

HTH
Steve
"Chris" <no****@nospam. be> wrote in message
news:7b******** *************** *********@4ax.c om...

What is the best way to convert a bytearray into a string ?

I thought I could do it using the code below, but is there anything
faster/easier than this? Thanks!

Chris

int aLength = 100000;
byte[] myByteArray = new byte[aLength ];
string myString = "";
for (int i=0; i<aLength ; i++)
{
myString += (char) myByteArray[i];
}

//work with this new string: cut out every char=='A'
myString.split( 'A');

...

Nov 17 '05 #2
Chris,

Do you know the encoding of the string? If so, then use the appropriate
encoder class (AsciiEncoder, UnicodeEncoder, etc, etc).

You would just call the GetString method on the encoder class at that
point.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Chris" <no****@nospam. be> wrote in message
news:7b******** *************** *********@4ax.c om...

What is the best way to convert a bytearray into a string ?

I thought I could do it using the code below, but is there anything
faster/easier than this? Thanks!

Chris

int aLength = 100000;
byte[] myByteArray = new byte[aLength ];
string myString = "";
for (int i=0; i<aLength ; i++)
{
myString += (char) myByteArray[i];
}

//work with this new string: cut out every char=='A'
myString.split( 'A');

...

Nov 17 '05 #3
First of all you are assuming that your array of bytes is an array of
chars, which might be risky.

However, String has a constructor that takes an array of characters.

Nov 17 '05 #4
Chris wrote:
What is the best way to convert a bytearray into a string ?


That all depends on the source of the byte array. What kind of data
does it represent? If it's text data, what encoding was used to encode
it? If it's arbitrary binary data (eg an mp3 file) you need to use
something like base64.

See http://www.pobox.com/~skeet/csharp/unicode.html

By the way, concatenating strings in a loop like that is a
performance-killer. See
http://www.pobox.com/~skeet/csharp/stringbuilder.html for more
information.

Jon

Nov 17 '05 #5
Thank you very much for these fast replies!

Chris

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2061
by: Kristina Engdahl | last post by:
Hello, I'm working with dynamic classloading and have encountered a problem when it comes to defining an URL for a file that is available through a bytearray representing a jar-file. The file in this case is a gif-file. I've implemented my own class loader which extends the java ClassLoader and overrides some methods for deriving classes from the jar-bytearray. Is there anyone who have any suggestion on how to create an URL-object from...
2
1499
by: Lou | last post by:
How do I convert a byteArray to a string?
13
12683
by: Ekim | last post by:
hy to all, I'm accessing a function from a managed-c++-dll within my c#-application. Therefore I encountered following troubles: In my c#-app I have got a byte-array, which should somehow be mapped to a char* in my managed-c++-function. Here is the prototype: ---------------------- managed c++ class library project ------------------------------- void MyFunction(char* buffer)
1
1338
by: Daniel von Fersen | last post by:
When I want to Read the Bytes 1000-2000 from a Stream into a ByteArray using Stream.Read(byteArray,1000,2000) they are written to the positions 1000-2000 in the byteArray. but my Array is only 1000 items long Array(0-999), and i just want to have the positions 1000-2000 from the stream! How can i realize it that the bytes 1000-2000 are written to an Array of
8
1254
by: Daniel von Fersen | last post by:
When I want to Read the Bytes 1000-2000 from a Stream into a ByteArray using Stream.Read(byteArray,1000,2000) they are written to the positions 1000-2000 in the byteArray. but my Array is only 1000 items long Array(0-999), and i just want to have the positions 1000-2000 from the stream! How can i realize it that the bytes 1000-2000 are written to an Array of
2
2737
by: B-Dog | last post by:
I'm trying to download a csv file into a datatable but can't seem to figure it out. I can read the csv into a bytearray and view it but I'm having trouble trying to figure out get the downloaded file into a datatable instead of saving the file to the hard drive then reading. I'd like to read it straight into the datatable. Any suggestions? Thanks
2
1071
by: BigC | last post by:
Hi, I am having problems converting from a Structure to a ByteArray. The main problem being that the structure contains a variable length array, which cannot be defined within the structure definition. A example of my code is shown below and when this is run, an error is produced within the BuildByteArray function due to the structure size not being set. Any help would be greatly appreciated.
1
2208
by: gijamie911 | last post by:
int nBytes = 256; byte ByteArray = new byte; int nBytesRead = inputStream.Read(ByteArray, 0 , nBytes); recorderID = ""; for (int i = 0; i <= 14; i++) { recorderID += char.ToString((char )ByteArray);
11
4057
by: Icemokka | last post by:
Hi, I'm need to upload a big file ( 600Mb+ ) to a BLOB field in MSSQL 2005. My code looks like this : fs = New FileStream(sFilePath, FileMode.Open) Dim ByteArray(fs.Length) As Byte fs.Read(ByteArray, 0, fs.Length)
0
9556
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10516
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10292
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10262
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10052
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9101
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
4156
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2959
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.