private byte[] hexStrTobyte(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).Trim(), 16);
return returnBytes;
}
private byte[] normalStrToHexByte(string str)
{
byte[] result = new byte[str.Length];
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str);
for (int i = 0; i < buffer.Length; i++)
{
result[i] = Convert.ToByte(buffer[i].ToString("X2"),16);
}
return result;
}
private string normalStrToHexStr(string str)
{
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str);
string result = string.Empty;
for (int i = 0; i < buffer.Length; i++)
{
result += buffer[i].ToString("X2") + " ";
}
return result;
}
C#实现数据转换小技巧
最新推荐文章于 2024-11-13 20:43:20 发布