PC Review


Reply
Thread Tools Rate Thread

Binary to string conversion

 
 
Diablo
Guest
Posts: n/a
 
      16th Mar 2005
Hi,

I am converting a byte array to string using
System.Text.Encoding.UTF8.GetString() or Unicode.GetString(). Then the
resulting text is converted back to binary, using the reverse function:
System.Text.Encoding.UTF8.GetBytes() or Unicode.GetBytes.
But the result does not match the original byte array!
Is the conversion loosing some characters? Why?

Thanks,
Diablo

 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      16th Mar 2005
Diablo <(E-Mail Removed)> wrote:
> I am converting a byte array to string using
> System.Text.Encoding.UTF8.GetString() or Unicode.GetString(). Then the
> resulting text is converted back to binary, using the reverse function:
> System.Text.Encoding.UTF8.GetBytes() or Unicode.GetBytes.
> But the result does not match the original byte array!
> Is the conversion loosing some characters? Why?


Presumably because the original binary data isn't a valid UTF-8 byte
sequence.

You shouldn't use encodings like this to convert arbitrary binary data
into text - use Convert.ToBase64String instead.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Diablo
Guest
Posts: n/a
 
      18th Mar 2005
Yes, you're right!

Thanks for the reply.

 
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
Hex to Binary Conversion Stephen Microsoft C# .NET 4 18th Jul 2007 04:49 PM
How to change binary data buffer read from a binary file to string format Anderson Microsoft VC .NET 1 21st Jul 2006 11:35 AM
SqlDataAdapter.Update(DataSet, string) -> String or binary data would be truncated. languy Microsoft C# .NET 1 1st Mar 2006 07:44 PM
SqlDataAdapter.Update(DataSet, string) -> String or binary data would be truncated. languy Microsoft C# .NET 0 1st Mar 2006 12:05 PM
Re: Hex to Binary conversion JulieJulie Microsoft Excel Programming 3 12th Sep 2003 02:18 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:46 AM.