将字符串转换为十六进制然后转换为字节数组
Converting String to Hex then into Byte Array
所以,
我有一个字符串,我想将每个字符转换为十六进制值,然后将其放入字节数组中以通过 com 端口发送。
我可以将单个字符转换为我需要发送的十六进制,但我无法将字符串数组正确地转换为字节数组。
示例:
string beforeConverting = "HELLO";
String[] afterConverting = {"0x48", "0x45", "0x4C", "0x4C", "0x4F"};
应该变成
byte[] byteData = new byte[]{0x48, 0x45, 0x4C, 0x4C, 0x4F};
我尝试了几个不同帖子中的几个不同的东西,但我无法将它们正确组合在一起。如果有人能指出我正确的方向或给我一段很棒的示例代码!
如果您的最终目标是发送 byte[]
,那么您实际上可以跳过中间步骤并立即使用 Encoding.ASCII.GetBytes
将 string
转换为 byte[]
(前提是您发送 ASCII 字符):
string beforeConverting = "HELLO";
byte[] byteData = Encoding.ASCII.GetBytes(beforeConverting);
//will give you {0x48, 0x45, 0x4C, 0x4C, 0x4F};
如果您不发送 ASCII,您可以根据需要找到合适的编码类型(如 Unicode 或 UTF32)。
也就是说,如果您仍想将十六进制字符串转换为字节数组,您可以这样做:
/// <summary>
/// To convert Hex data string to bytes (i.e. 0x01455687) given the data type
/// </summary>
/// <param name="hexString"></param>
/// <param name="dataType"></param>
/// <returns></returns>
public static byte[] HexStringToBytes(string hexString) {
try {
if (hexString.Length >= 3) //must have minimum of length of 3
if (hexString[0] == '0' && (hexString[1] == 'x' || hexString[1] == 'X'))
hexString = hexString.Substring(2);
int dataSize = (hexString.Length - 1) / 2;
int expectedStringLength = 2 * dataSize;
while (hexString.Length < expectedStringLength)
hexString = "0" + hexString; //zero padding in the front
int NumberChars = hexString.Length / 2;
byte[] bytes = new byte[NumberChars];
using (var sr = new StringReader(hexString)) {
for (int i = 0; i < NumberChars; i++)
bytes[i] = Convert.ToByte(new string(new char[2] { (char)sr.Read(), (char)sr.Read() }), 16);
}
return bytes;
} catch {
return null;
}
}
然后像这样使用它:
byte[] byteData = afterConverting.Select(x => HexStringToBytes(x)[0]).ToArray();
我上面的方法比较通用,可以像0x05163782
一样处理输入string
给byte[4]
。对于您的使用,您只需要取第一个字节(因为 byte[]
将始终是 byte[1]
),因此您在 LINQ Select
.[=26 中有 [0]
索引=]
上面自定义方法中使用的核心方法是Convert.ToByte()
:
bytes[i] = Convert.ToByte(new string(new char[2] { (char)sr.Read(), (char)sr.Read() }), 16);
要仅将十六进制字符串转换为数字,您可以像这样使用 System.Convert class
string hex = "0x3B";
byte b = Convert.ToByte(hex.Substring(2), 16)
// b is now 0x3B
子串用于跳过字符0x
所以, 我有一个字符串,我想将每个字符转换为十六进制值,然后将其放入字节数组中以通过 com 端口发送。
我可以将单个字符转换为我需要发送的十六进制,但我无法将字符串数组正确地转换为字节数组。
示例:
string beforeConverting = "HELLO";
String[] afterConverting = {"0x48", "0x45", "0x4C", "0x4C", "0x4F"};
应该变成
byte[] byteData = new byte[]{0x48, 0x45, 0x4C, 0x4C, 0x4F};
我尝试了几个不同帖子中的几个不同的东西,但我无法将它们正确组合在一起。如果有人能指出我正确的方向或给我一段很棒的示例代码!
如果您的最终目标是发送 byte[]
,那么您实际上可以跳过中间步骤并立即使用 Encoding.ASCII.GetBytes
将 string
转换为 byte[]
(前提是您发送 ASCII 字符):
string beforeConverting = "HELLO";
byte[] byteData = Encoding.ASCII.GetBytes(beforeConverting);
//will give you {0x48, 0x45, 0x4C, 0x4C, 0x4F};
如果您不发送 ASCII,您可以根据需要找到合适的编码类型(如 Unicode 或 UTF32)。
也就是说,如果您仍想将十六进制字符串转换为字节数组,您可以这样做:
/// <summary>
/// To convert Hex data string to bytes (i.e. 0x01455687) given the data type
/// </summary>
/// <param name="hexString"></param>
/// <param name="dataType"></param>
/// <returns></returns>
public static byte[] HexStringToBytes(string hexString) {
try {
if (hexString.Length >= 3) //must have minimum of length of 3
if (hexString[0] == '0' && (hexString[1] == 'x' || hexString[1] == 'X'))
hexString = hexString.Substring(2);
int dataSize = (hexString.Length - 1) / 2;
int expectedStringLength = 2 * dataSize;
while (hexString.Length < expectedStringLength)
hexString = "0" + hexString; //zero padding in the front
int NumberChars = hexString.Length / 2;
byte[] bytes = new byte[NumberChars];
using (var sr = new StringReader(hexString)) {
for (int i = 0; i < NumberChars; i++)
bytes[i] = Convert.ToByte(new string(new char[2] { (char)sr.Read(), (char)sr.Read() }), 16);
}
return bytes;
} catch {
return null;
}
}
然后像这样使用它:
byte[] byteData = afterConverting.Select(x => HexStringToBytes(x)[0]).ToArray();
我上面的方法比较通用,可以像0x05163782
一样处理输入string
给byte[4]
。对于您的使用,您只需要取第一个字节(因为 byte[]
将始终是 byte[1]
),因此您在 LINQ Select
.[=26 中有 [0]
索引=]
上面自定义方法中使用的核心方法是Convert.ToByte()
:
bytes[i] = Convert.ToByte(new string(new char[2] { (char)sr.Read(), (char)sr.Read() }), 16);
要仅将十六进制字符串转换为数字,您可以像这样使用 System.Convert class
string hex = "0x3B";
byte b = Convert.ToByte(hex.Substring(2), 16)
// b is now 0x3B
子串用于跳过字符0x