Converting from hex to string

后端 未结 6 703
慢半拍i
慢半拍i 2020-11-27 04:44

I need to check for a string located inside a packet that I receive as byte array. If I use BitConverter.ToString(), I get the bytes a

6条回答
  •  被撕碎了的回忆
    2020-11-27 05:15

    If you need the result as byte array, you should pass it directly without changing it to a string, then change it back to bytes. In your example the (f.e.: 0x31 = 1) is the ASCII codes. In that case to convert a string (of hex values) to ASCII values use: Encoding.ASCII.GetString(byte[])

            byte[] data = new byte[] { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30 };
            string ascii=Encoding.ASCII.GetString(data);
            Console.WriteLine(ascii);
    

    The console will display: 1234567890

提交回复
热议问题