0

hi guys i want to convert from ascii code to normal code cause i made a method that converts from normal code to ascii code and i want to revers it here is my method to convert to ascii

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;
    }
Ahmed Kamal
  • 89
  • 3
  • 10
  • Not merely a duplicate but practically the same title, down to the word "normal". Surprised it didn't turn up in the search you performed before asking the question. – Raymond Chen Feb 23 '13 at 16:19

1 Answers1

0

The string you are creating is irreversible. Look at this example from 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

If you put this numbers together you will get 0114168255. No way you can get bytes back from it. What you can do is to use Byte.ToString("x") instead. It will produce hexadecimal representation of the underlying byte value. It will be always of lenght 2.

Dzienny
  • 3,295
  • 1
  • 20
  • 30