473,322 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Byte array to string

How to convert this byte array to string
byte[] b=new byte[100];

Is there any function or I need read one by one and build the string

thanks
Nov 17 '05 #1
3 276649

"pkumar" <pk****@discussions.microsoft.com.> wrote in message
news:AE**********************************@microsof t.com...
How to convert this byte array to string
byte[] b=new byte[100];

Is there any function or I need read one by one and build the string

thanks


byte[] b = new byte[100];
string s = System.Text.ASCIIEncoding.ASCII.GetString(b);
HTH,
Mythran

Nov 17 '05 #2
Be carrefull with System.Text.ASCIIEncoding.ASCII.GetString(b);

If your codepage is different than English then the conversion could have
problems (not always).

I had this problem where I had a program running on Korea where the codepage
where different and then the range of Extended ASCII characters supported
are from 0 to 239, instead 0 to 255.

I experimented this problem with some Windows installed on different
languages.

If you want get a string back you can use something like this.

System.Text.Encoding.GetEncoding(1251).GetString(b ); (1251 is English)

You can see how the conversion fail if you go to Control Panel, Regional
Settings, Advanced and on "Language for non-Unicode programs" put Japanese.

The strange thing is: clearly it says: "Language for non-Unicode programs",
I know .net is full Unicode.

Now, why using System.Text.ASCIIEncoding.ASCII fail?, I don't know that...

Really doesn't fail, but if you Encode a string with extended characters
into bytes and Decoded again into string, you will get different results. I
guess .net map the extended character to the near one supported for the
codepage.

Gustavo.
"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:OA**************@TK2MSFTNGP12.phx.gbl...

"pkumar" <pk****@discussions.microsoft.com.> wrote in message
news:AE**********************************@microsof t.com...
How to convert this byte array to string
byte[] b=new byte[100];

Is there any function or I need read one by one and build the string

thanks


byte[] b = new byte[100];
string s = System.Text.ASCIIEncoding.ASCII.GetString(b);
HTH,
Mythran

Nov 17 '05 #3
Franco, Gustavo <gustavo_franco[REMOVEIT]@hotmail.com> wrote:
Be carrefull with System.Text.ASCIIEncoding.ASCII.GetString(b);

If your codepage is different than English then the conversion could have
problems (not always).

I had this problem where I had a program running on Korea where the codepage
where different and then the range of Extended ASCII characters supported
are from 0 to 239, instead 0 to 255.
Careful here - there's no such encoding as "Extended ASCII". There are
various character encodings which *are* extensions to ASCII, but no one
"extended ASCII".
I experimented this problem with some Windows installed on different
languages.

If you want get a string back you can use something like this.

System.Text.Encoding.GetEncoding(1251).GetString(b ); (1251 is English)
1251 is just *one* code page...
You can see how the conversion fail if you go to Control Panel, Regional
Settings, Advanced and on "Language for non-Unicode programs" put Japanese.

The strange thing is: clearly it says: "Language for non-Unicode programs",
I know .net is full Unicode.
Yes, but the point is that a byte array isn't an array of characters.
You need to know what encoding the bytes represent characters in, in
order to get from them to Unicode.
Now, why using System.Text.ASCIIEncoding.ASCII fail?, I don't know that...
Because any byte values greater than 127 aren't ASCII.
Really doesn't fail, but if you Encode a string with extended characters
into bytes and Decoded again into string, you will get different results. I
guess .net map the extended character to the near one supported for the
codepage.


You're asking the encoding to deal with bytes which it can't handle -
that's why things go wrong.

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

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

Similar topics

11
by: Dan C | last post by:
Is there a routine in c# that will transform a string ie"Hello Mom" into a Byte array. I have found char cTmp = pString.ToCharArray(); But I have not been able to figure out how to convert a...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
4
by: David Bargna | last post by:
Hi I have a problem, I have a string which needs to be converted to a byte array, then have the string representation of this array stored in an AD attribute. This string attribute then has to...
2
by: Bryan | last post by:
Apologies if this is a noob question, but I've been struggling with this for quite a while... I'm trying to convert a byte array (encrypted authorization code) into a *screen-printable* string...
5
by: Terry Olsen | last post by:
Looking for info on how to convert a byte array to a string, and string to byte array. Thanks.
6
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how...
3
by: marfi95 | last post by:
Hi all. I need to copy a byte array into a string, but starting at a specific location in the byte array. This is where I get hung up. For example if my byte array is (100) big, I might want to...
4
by: ThunderMusic | last post by:
Hi, I have to go from Byte() to String, do some processing then reconvert the String to byte() but using ascii format, not unicode. I currently use a stream to write the char()...
10
by: Danny | last post by:
I am working on a project where I will receive xml documents from clients machines as a byte array. They will use the web browser navigate method to post the data to my ASP.NET page. I then pick up...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.