PC Review


Reply
Thread Tools Rate Thread

Converting from byte to string and back to byte ends in different results?

 
 
mfunkmann@yahoo.com
Guest
Posts: n/a
 
      17th Dec 2006
Hi there,

I have a really strange conversion-problem here:

--
string _content = new string("some data");

...

System.Text.Encoding _Convert= new System.Text.ASCIIEncoding();
byte[] _encrypted =
_Encrypt.TransformFinalBlock(_Convert.GetBytes(_content), 0,
_Convert.GetByteCount(_content));
_content = _Convert.GetString(_encrypted);
byte[] _t = _Convert.GetBytes(_content);
--

_t differs from _enrypted

I another testprogram I have following code:

--
System.Text.Encoding _Convert = new System.Text.ASCIIEncoding();
byte[] test = { 44, 59, 56, 78, 67, 89, 67, 86 };
text = _Convert.GetString(test);
byte[] _new = _Convert.GetBytes(text);
--

and here _new is equal to test!



Even pasting the code from the testprogram and changing the parameters
accordingly did not work.


I really don't know what to do anymore..

Any ideas?

 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      17th Dec 2006
<(E-Mail Removed)> wrote:
> I have a really strange conversion-problem here:


It's not really strange at all.

Encryption gives arbitrary binary data. Converting that into ASCII is a
lossy operation, as ASCII only defines values under 128. You shouldn't
treat arbitrary binary data as if it were encoded text. Use something
like Base64 instead (see Convert.ToBase64String and
Convert.FromBase64String).

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.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
vb.net:converting from byte array to string and back again movieknight@gmail.com Microsoft Dot NET 4 8th Apr 2006 09:48 AM
MD5 Hash - Converting Result From Byte[] Back To String dlarock@gmail.com Microsoft C# .NET 7 24th Oct 2005 10:48 PM
How to convert byte() to string, and from string back to byte() moondaddy Microsoft VB .NET 4 15th Aug 2005 03:25 PM
Problem converting byte() to string and then back to byte() moondaddy Microsoft VB .NET 8 3rd May 2005 07:23 AM
Convert from managed byte[] to void* and back to byte[] again jim.wiese@gmail.com Microsoft Dot NET 1 15th Feb 2005 06:11 AM


Features
 

Advertising
 

Newsgroups
 


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