我想将十六进制字符串转换为0x()形式。 [英] I want to convert hexadecimal string to the form 0x() form.
本文介绍了我想将十六进制字符串转换为0x()形式。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
string input = 019F314A
我希望byte []字节等于{0x01,0x9F,0x31,0x4A}
。因为我想制作一个帧格式需要这样的数据。
提前致谢。
我尝试了什么:
string input = 019F314A
I want byte[] bytes to equal { 0x01, 0x9F, 0x31, 0x4A }
.Because i want to make a frame format which required data like that.
Thanks in advance.
What I have tried:
public static byte[] StringToByteArray(String hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}
推荐答案
0x
只是表示代表的符号数字为十六进制
您的函数将返回输出数组为{1,159,49,74}
这是一个整数数组,并且等效于每个字节符号
0x
is just a notation to represent the number asHex
your function will return the output array as{1,159,49,74}
which is an integer array and as well as equivalent to the each byte notation
01 -> 1
9F -> 159
31 -> 49
4A -> 74
假设您希望该值为十六进制表示法,那么您必须将该值转换为字符串为
suppose if you want the value to be in Hex notation then you will have to convert the value to string as
string[] HexString = result.ToList().Select(k => "0x" + k.ToString("x")).ToArray();
// { "0x1","0x9F", "0x31", "0x4A" }
哪些没用,再一次你必须将它转换成字节才能使用。
参考此在十六进制字符串和数字类型之间转换[ ^ ]
这篇关于我想将十六进制字符串转换为0x()形式。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文