473,507 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting Between byte[] and String?

How does one convert between byte[] and string?

MF
May 26 '06 #1
9 4018
Matt F wrote:
How does one convert between byte[] and string?


Will this help?

ASCIIEncoding encoding = new ASCIIEncoding();
char[] charArray = new char[encoding.GetCharCount(byteArray, 0, byteArray.Length)];
encoding.GetDecoder().GetChars(byteArray, 0, byteArray.Length, charArray, 0);
string s = new string(charArray);
May 26 '06 #2
string to byte[]:

byte[] b = Encoding.ASCII.GetBytes("Hello World");

byte[] to string:

string s = Encoding.ASCII.GetString(b);
Of course you should use the appropriate encoding in place of ASCII in
my examples.

May 26 '06 #3
You use the appropriate System.Text.Decoder class to convert the array to
the Text encoding that it represents/contains. This is typically done by
using an instance of the appropriate System.Text.Encoding class, and calling
the GetDecoder() method. Of course, you have to know what encoding was used
to create the byte array.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

This is, by definition, not that.

"Matt F" <ma********************@gmail.com> wrote in message
news:o8Edg.75169$IZ2.51400@dukeread07...
How does one convert between byte[] and string?

MF

May 26 '06 #4
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
You use the appropriate System.Text.Decoder class to convert the array to
the Text encoding that it represents/contains. This is typically done by
using an instance of the appropriate System.Text.Encoding class, and calling
the GetDecoder() method. Of course, you have to know what encoding was used
to create the byte array.


Why use a Decoder explicitly, when Encoding.GetString will do it for
you? You only need a Decoder if you're decoding multiple chunks which
may introduce an issue of half of an encoded character being in one
chunk, and half being in the other chunk.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 26 '06 #5
Matt F <ma********************@gmail.com> wrote:
How does one convert between byte[] and string?


That entirely depends on the contents of the byte array, and how you
want to represent it.

The other answers in this thread have focused on the situation where
the byte array contains text which has been encoded with a particular
character encoding. If, however, your byte array contains arbitrary
binary data, you should consider using Convert.ToBase64String and
Convert.FromBase64String.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 26 '06 #6
Sericinus hunter <se*****@flash.net> wrote:
Matt F wrote:
How does one convert between byte[] and string?


Will this help?

ASCIIEncoding encoding = new ASCIIEncoding();
char[] charArray = new char[encoding.GetCharCount(byteArray, 0, byteArray.Length)];
encoding.GetDecoder().GetChars(byteArray, 0, byteArray.Length, charArray, 0);
string s = new string(charArray);


That looks like a very long way of writing:

string s = Encoding.ASCII.GetString(byteArray);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 26 '06 #7
The qyestion was asked in a geneneral way, so I have a less targeted
approach, which show the underling process, rather than a specific situation
and a simplified soltution for that situation.

Quite often, the solutin is indeed simle, but wihtout a specific enough set
of rules to establish the specific need, I am left to drop to more
elelementary explanation of the underling technology, from which I hop the
OP to glean the spedific implementation.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

This is, by definition, not that.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
You use the appropriate System.Text.Decoder class to convert the array to
the Text encoding that it represents/contains. This is typically done by
using an instance of the appropriate System.Text.Encoding class, and
calling
the GetDecoder() method. Of course, you have to know what encoding was
used
to create the byte array.


Why use a Decoder explicitly, when Encoding.GetString will do it for
you? You only need a Decoder if you're decoding multiple chunks which
may introduce an issue of half of an encoded character being in one
chunk, and half being in the other chunk.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

May 28 '06 #8
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
The qyestion was asked in a geneneral way, so I have a less targeted
approach, which show the underling process, rather than a specific situation
and a simplified soltution for that situation.

Quite often, the solutin is indeed simle, but wihtout a specific enough set
of rules to establish the specific need, I am left to drop to more
elelementary explanation of the underling technology, from which I hop the
OP to glean the spedific implementation.


While I see your point, I would say that probably 99% of the time
people *don't* need to use a Decoder - and that using a Decoder
explicitly will make their code more complicated.

I can't remember the last time I needed to use Decoder rather than just
calling Encoding.GetString - I suspect it may have been when I was
writing my own version of BinaryReader, which is far from a normal
situation :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 28 '06 #9
Boy, I must have been tired when I wrote my reply! I can't believe how many
speeling errors it contained! I'm so ashamed!

BTW, I too almost never use a Decoder explicitly!

--
;-),

Kevin Spencer
Microsoft MVP
Professional Numbskull

This is, by definition, not that.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
The qyestion was asked in a geneneral way, so I have a less targeted
approach, which show the underling process, rather than a specific
situation
and a simplified soltution for that situation.

Quite often, the solutin is indeed simle, but wihtout a specific enough
set
of rules to establish the specific need, I am left to drop to more
elelementary explanation of the underling technology, from which I hop
the
OP to glean the spedific implementation.


While I see your point, I would say that probably 99% of the time
people *don't* need to use a Decoder - and that using a Decoder
explicitly will make their code more complicated.

I can't remember the last time I needed to use Decoder rather than just
calling Encoding.GetString - I suspect it may have been when I was
writing my own version of BinaryReader, which is far from a normal
situation :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

May 28 '06 #10

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

Similar topics

5
13900
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
4
10277
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with...
4
5358
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
8
4533
by: iyuen | last post by:
I'm having problems with converting a byte array to an image object~ My byte array is an picture in VB6 StdPicture format. I've used propertybag to convert the picture into base64Array format in...
8
3655
by: Marius Cabas | last post by:
Hi, I'm a beginner so don't shoot ;) I'm reading a wave file into a byte and I'm trying to convert the result to String but the converted string is altered, so if I'm generating a new wave file...
7
17011
by: dlarock | last post by:
I wrote the following to do an MD5 hash. However, I have a problem (I think) with the conversion from the Byte MD5 hash back to string. Watching this through the debugger it appears as if the...
5
4158
by: David | last post by:
I note that you can null teminate a string by adding controlchar.null. Is there a way of adding a null to a Buffer of Bytes and converting it to a string. I have packets coming in from a...
8
4178
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
16
5421
by: manmit.walia | last post by:
Hello All, I have tried multiple online tools to convert an VB6 (bas) file to VB.NET file and no luck. I was hoping that someone could help me covert this. I am new to the .NET world and still...
6
8365
by: ogtheterror | last post by:
Hi I have a very limited understanding of Python and have given this the best shot i have but still have not been able to get it working. Is there anyone that knows how to get this into a .net...
0
7223
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,...
0
7114
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
7321
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
7377
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...
0
7488
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...
1
5045
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...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
412
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...

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.