473,395 Members | 1,649 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,395 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 276660

"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.