4

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.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
sam byte
  • 697
  • 9
  • 11

5 Answers5

8

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

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

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Fratyx
  • 5,717
  • 1
  • 12
  • 22
4

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);
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

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.

Piyush Parashar
  • 866
  • 8
  • 20
0

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
sam byte
  • 697
  • 9
  • 11
  • this works yes if you dont actualy want the string to be represented in ascii. – Vajura Nov 24 '14 at 07:56
  • I tested it with a very long string (text), in fact copying half a page. Convert it to bytes array then back to string. I can get the original text... Just watch out about this simple mistake. It can lead to severe headache, at least for me... ;) – sam byte Nov 24 '14 at 08:03
0

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); 
Vajura
  • 1,112
  • 7
  • 16