PC Review


Reply
Thread Tools Rate Thread

Converting from byte[] to string and viceversa???

 
 
Bob Rock
Guest
Posts: n/a
 
      14th May 2004
Hello,

a simple question (if you know the answer) ... how to convert from and array
of bytes to a string (and viceversa). I haven't the conversion to be simple
... but even using the Convert or the Encoding class I seem to be having
trouble accomplishing it.


Bob Rock




 
Reply With Quote
 
 
 
 
Dennis Myrén
Guest
Posts: n/a
 
      14th May 2004
I would use Encoding.Default.GetBytes for the String->Byte [] conversion

and Encoding.Default.GetString for the Byte []->String conversion

"Bob Rock" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
>
> a simple question (if you know the answer) ... how to convert from and

array
> of bytes to a string (and viceversa). I haven't the conversion to be

simple
> .. but even using the Convert or the Encoding class I seem to be having
> trouble accomplishing it.
>
>
> Bob Rock
>
>
>
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      14th May 2004
Bob Rock <(E-Mail Removed)> wrote:
> a simple question (if you know the answer) ... how to convert from and array
> of bytes to a string (and viceversa). I haven't the conversion to be simple
> .. but even using the Convert or the Encoding class I seem to be having
> trouble accomplishing it.


You need to know what encoding to use.

See http://www.pobox.com/~skeet/csharp/unicode.html for an explanation
of what it's all about.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      14th May 2004
Dennis Myrén <(E-Mail Removed)> wrote:
> I would use Encoding.Default.GetBytes for the String->Byte [] conversion
>
> and Encoding.Default.GetString for the Byte []->String conversion


That's only applicable if you *do* want to use the default encoding for
the current computer though - which in many cases isn't the right
choice. Guessing isn't a good idea with character encodings.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Dennis Myrén
Guest
Posts: n/a
 
      14th May 2004
Hehe.

Yes, i know that.
It was an _example_.

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
Dennis Myrén <(E-Mail Removed)> wrote:
> I would use Encoding.Default.GetBytes for the String->Byte [] conversion
>
> and Encoding.Default.GetString for the Byte []->String conversion


That's only applicable if you *do* want to use the default encoding for
the current computer though - which in many cases isn't the right
choice. Guessing isn't a good idea with character encodings.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


 
Reply With Quote
 
Sahil Malik
Guest
Posts: n/a
 
      14th May 2004
System.Byte[] inputData;
string inputstream = System.Text.Encoding.ASCII.GetString(inputData) ;

Thats it !!

- Sahil Malik
Independent Consultant
You can reach me thru my blog - http://dotnetjunkies.com/WebLog/sahilmalik/



"Bob Rock" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
>
> a simple question (if you know the answer) ... how to convert from and

array
> of bytes to a string (and viceversa). I haven't the conversion to be

simple
> .. but even using the Convert or the Encoding class I seem to be having
> trouble accomplishing it.
>
>
> Bob Rock
>
>
>
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      14th May 2004
Sahil Malik <(E-Mail Removed)> wrote:
> System.Byte[] inputData;
> string inputstream = System.Text.Encoding.ASCII.GetString(inputData) ;
>
> Thats it !!


Again, that's it if Encoding.ASCII *happens* to be the encoding you
want.

The code is going to be simple virtually whatever encoding you're
interested in, but working out which encoding is the right one to use
is very important - much more so than your post implied.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting from byte to string and back to byte ends in different results? mfunkmann@yahoo.com Microsoft C# .NET 1 17th Dec 2006 02:53 PM
Converting Between byte[] and String? Matt F Microsoft C# .NET 9 28th May 2006 05:36 PM
Problem converting byte() to string and then back to byte() moondaddy Microsoft VB .NET 8 3rd May 2005 07:23 AM
Converting from byte[] to string and viceversa??? Bob Rock Microsoft C# .NET 6 14th May 2004 05:56 PM
Converting from byte[] to string and viceversa??? Bob Rock Microsoft Dot NET Framework 6 14th May 2004 05:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:31 AM.