473,545 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 276672

"pkumar" <pk****@discuss ions.microsoft. com.> wrote in message
news:AE******** *************** ***********@mic rosoft.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.ASC IIEncoding.ASCI I.GetString(b);
HTH,
Mythran

Nov 17 '05 #2
Be carrefull with System.Text.ASC IIEncoding.ASCI I.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.Enc oding.GetEncodi ng(1251).GetStr ing(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.ASC IIEncoding.ASCI I 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********@hot mail.comREMOVET RAIL> wrote in message
news:OA******** ******@TK2MSFTN GP12.phx.gbl...

"pkumar" <pk****@discuss ions.microsoft. com.> wrote in message
news:AE******** *************** ***********@mic rosoft.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.ASC IIEncoding.ASCI I.GetString(b);
HTH,
Mythran

Nov 17 '05 #3
Franco, Gustavo <gustavo_fran co[REMOVEIT]@hotmail.com> wrote:
Be carrefull with System.Text.ASC IIEncoding.ASCI I.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.Enc oding.GetEncodi ng(1251).GetStr ing(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.ASC IIEncoding.ASCI I 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.co m>
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
30555
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 char into a hex value and place that value into the byte. Thanks for your help
3
9498
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 the end of this message is the inflate method this is where I get stuck I know that I need a byte array but because I am decompressing a string I...
8
10696
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 receive a byte array as one of its parameters. The project is marked for COM interop, and that all proceeds normally. When I reference the type...
4
13391
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 be read and the string representation of the byte array has to be converted back to the original byte array and converted back to the original string...
2
3566
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 that is displayed in a text box. Once displayed, the text will be copied, transmitted and then pasted (all manually by humans) into a second utility...
5
4182
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
53687
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 do I convert it to: dim myStr as string = "11,22,33,44"
3
12782
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 start at position 60 for example and copy from 60 to the next null byte in the array to my string. The starting position is variable, as is where...
4
3385
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() (BinaryWriter.Write) from the string (String.ToCharArray), then use Stream.ToArray to convert everything to byte(). It works most of the time, but it happens that an...
10
2661
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 the byte array using the request object (XMLData=bytearray..). Can someone point me to an article that shows me how I can do this in ASP.NET, C#? ...
0
7478
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7410
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...
1
7437
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...
0
7773
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...
0
5984
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...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
1901
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
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
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...

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.