新手刚接触C#不久,想问下假如一个字符串里假如是16进制的数据“F3AE56FF”,怎么样存入byte[]数组bytes中,例如说bytes[0]=F3,bytes[1]=AE,bytes[2]=56,bytes[3]=FF这样……
解决方案
30
先在左侧填充0,以使字符串长度为偶数位,然后直接按两位长度截取,最后Convert
25
string text = @"F3AE56FF"; if (text.Length % 2 != 0) { text = "0" + text; } byte[] bytes = new byte[text.Length / 2]; for (var i = 0; i < text.Length; i += 2) { bytes[i / 2] = Convert.ToByte(text.Substring(i, 2), 16); } Console.WriteLine(string.Join(",", bytes));