如何将ascii代码转换为c#中的正常代码?

问题描述:

嗨,大家好,我想从ASCII码转换成普通代码 因为我做的是从正常的代码转换为ASCII码的方法,我想在这里REVERS它 是我的方法来转换为ASCII如何将ascii代码转换为c#中的正常代码?

public string CreatSerial() 
    { 
     string serial = ""; 
     string P1, P2 = ""; 
     string Befor_Serial = GetDeviceSerial().Trim() + 5; 
     P1 = Befor_Serial.Substring(0, Befor_Serial.Length/2); 
     P2 = Befor_Serial.Substring(Befor_Serial.Length/2); 
     Befor_Serial = P2 + "ICS" + P1; 
     char[] strarr = Befor_Serial.ToCharArray(); 
     Array.Reverse(strarr); 
     serial = new string(strarr); 
     byte[] asciiBytes = Encoding.ASCII.GetBytes(serial); 
     serial = ""; 
     foreach (byte b in asciiBytes) 
     { 
      serial += b.ToString(); 
     } 
     return serial; 
    } 
+1

什么是“正常”? – 2013-02-23 16:07:32

+0

不仅仅是一个重复但实际上相同的标题,直到“正常”这个词。惊讶的是它没有出现在你提问之前进行的搜索中。 – 2013-02-23 16:19:48

您创建的字符串是不可逆的。从MSDN

byte[] bytes = {0, 1, 14, 168, 255}; 
foreach (byte byteValue in bytes) 
    Console.WriteLine(byteValue.ToString()); 
// The example displays the following output to the console if the current 
// culture is en-US: 
//  0 
//  1 
//  14 
//  168 
//  255 

请看下面的例子。如果你把这个数字加在一起,你会得到0114168255。没办法从中获得字节。你可以做的是使用Byte.ToString("x")来代替。它将产生底层字节值的十六进制表示。它总是长度为2.