Byte error before string for byte conversion

Can anyone help spot the error? Here is 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 not return to the original byte values 0xB0, 0x2D.

+4
source share
5 answers

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

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

+8
source

This is because .NET does not support the Extended ASCII table . Each value above 127 will produce ?what makes up 63.

, ? 63.

UTF8 , , newBytes 4 2:

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

[] 0xB0 176 0x2D 45. ASCII, 128 176, ? (undefined) 45 -.

, .

+1

ahaah.. ! Encoding.Unicode ASCII. ...;)

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

.Net ascii. , char, int, .

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

All Articles