0

こんにちはみんな私はアスキーコードから通常のコードに変換したいので私は通常のコードからアスキーコードに変換するメソッドを作りましたそして私はそれを逆にしたいですここで私はアスキーに変換する私の方法です

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;
    }
4

1 に答える 1

0

作成している文字列は元に戻せません。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")代わりに使用することです。基になるバイト値の 16 進数表現を生成します。常に長さ 2 になります。

于 2013-02-23T16:26:43.220 に答える