Byte to String to byte conversion puzzling error

679 views Asked by At

Could anyone please help spot the error? Here's the code:

   byte[] oriBytes = { 0xB0, 0x2D };                       // oriBytes -> 0xB0, 0x2D
   string oriInStr = Encoding.ASCII.GetString(oriBytes);   // oriInStr ->   "?-"
   oriBytes = Encoding.ASCII.GetBytes(oriInStr);           // oriBytes -> 0x3F, 0x2D

I can't get back the original bytes values of 0xB0, 0x2D.

5

There are 5 answers

0
Fratyx On

0xB0 is not a valid ASCII code. You can read here:

Any byte greater than hexadecimal 0x7F is decoded as the Unicode question mark ("?")

0
Piyush Parashar On

In you byte[] 0xB0 changes to 176 and 0x2D change to 45. When converted from ASCII which has only 128 character 176 will give you ? (undefined) and 45 gives you -.

Try to debug the code and see what is happening.

0
Patrick Hofman On

That is because appearantly .NET doesn't support the Extended ASCII table. Every value above 127 will produce ?, which is 63.

Hence, converting the ? back will result in 63.

When running the code with UTF8 encoding, you will see it goes to the extended page, since the newBytes in this sample returns 4 bytes instead of 2:

byte[] oriBytes = { 0xB0, 0x2D };
string oriInStr = Encoding.UTF8.GetString(oriBytes);
byte[] newBytes = Encoding.UTF8.GetBytes(oriInStr);
2
sam byte On

ahaah.. I got it! use Encoding.Unicode instead of ASCII. Beware guys... ;)

   byte[] oriBytes = { 0xB0, 0x2D };                         // oriBytes -> 0xB0, 0x2D
   string oriInStr = Encoding.Unicode.GetString(oriBytes);   // oriInStr ->   "?-"
   oriBytes = Encoding.Unicode.GetBytes(oriInStr);           // oriBytes -> 0xB0, 0x2D
2
Vajura On

As others have mentioned .Net doesnt support extended ascii. To solve this problem you should cast the byte values to char which is essentialy int and it will map them correctly.

 byte[] oriBytes = { 0xB0, 0x2D };                      
 string oriInStr = "";
 for (int a = 0; a < oriBytes.Length; a++)
     oriInStr += (char)(oriBytes[a]);
 oriBytes = Encoding.ASCII.GetBytes(oriInStr);