今天开发过程中后台给了我一个已经转成string了的16进制字符串,我这边做了些处理,将str又转回16进制,并以16位16进制byte数组的形式存放了。
void Start()
{
string str = "qqwweerrttyyuuiiooppaassddffgghh"
print (strToToHexByte(str));
}
/// <summary>
/// 字符串转16进制字节数组
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
private static byte[] strToToHexByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}