将十六进制字节的字符串表示形式转换为字节

时间:2020-06-06 20:27:11

标签: c#

我已经在论坛中搜索并获得了多个答案,但没有找到我所能解决的问题...

我有字符串:“ 48FF0015E8”,我需要将其转换为字节数组,例如 字节[] {48,FF,00,15,E8}, 与字符串相同,没有转换为其他内容...

使用字节bt = Convert.ToByte(“ E8”,16)可以完成此任务,但是由于Convert.ToByte(“ 48”,16)在数组中存储“ 72”时会失败

我需要以2个字符(1个字节)的块为单位循环字符串,因为我需要进行一些比较然后存储在字节数组中。

for(int x = 0;x<numBytes;x+=2)
{
  string strByte = HexStr.Substring(x, 2) //Loop bytes from string "48FF0015E8" 
  byte bt = Convert.ToByte(strByte, 16); // here is where i have problems
  offset += 2;
}

1 个答案:

答案 0 :(得分:0)

您可以尝试

<attr name="onItemClick" format="string"/>

https://dotnetfiddle.net/H5cFOk