十六进制也字节数组转换问题,我想知道原理是怎么样的
public static byte[] HexStringToByteArray(string s)
{
Byte[] buf = new byte[s.Length / 2];
for (int i = 0; i < buf.Length; i++)
{
buf[i] = (byte)(chr2hex(s.Substring(i * 2, 1)) * 0x10 + chr2hex(s.Substring(i * 2 + 1, 1)));
}
return buf;
}
private static byte chr2hex(string chr)
{
switch (chr)
{
case "0":
return 0x00;
case "1":
return 0x01;
case "2":
return 0x02;
case "3":
return 0x03;
case "4":
return 0x04;
case "5":
return 0x05;
case "6":
return 0x06;
case "7":
return 0x07;
case "8":
return 0x08;
case "9":
return 0x09;
case "A":
return 0x0a;
case "B":
return 0x0b;
case "C":
return 0x0c;
case "D":
return 0x0d;
case "E":
return 0x0e;
case "F":
return 0x0f;
}
return 0x00;
}
buf[i] = (byte)(chr2hex(s.Substring(i * 2, 1)) * 0x10 + chr2hex(s.Substring(i * 2 + 1, 1)));
不明白这句话是什么意思,谁能解释一下越通俗越好,谢谢