【问题标题】:C# - Convert hex string to byte array of hex valuesC# - 将十六进制字符串转换为十六进制值的字节数组
【发布时间】:2017-06-17 05:42:41
【问题描述】:

这个问题看起来像一堆其他问题,但没有一个完全符合我的需要。其他相关问题的问题似乎是基于 Visual Studio 的 IntelliSense 隐式转换为十进制。

目标:尝试在 C# 中将十六进制字符串转换为十六进制值(不是十进制值)的字节数组。

public static byte[] ConvertHexValueToByteArray() 
{
  string hexIpAddress = "0A010248"; // 10.1.2.72 => "0A010248"
  byte[] bytes = new byte[hexIpAddress.Length / 2];

  for (int i = 0; i < hexIpAddress.Length; i += 2)
  {
    string s2CharSubStr = hexIpAddress.Substring(i, 2);  // holds "0A" on 1st pass, "01" on 2nd pass, etc.

    if ((s2CharSubStr.IsAllDigit()) && (int.Parse(s2CharSubStr) < 10)) // fixes 0 to 9
      bytes[i / 2] = (byte) int.Parse(s2CharSubStr); // same value even if translated to decimal
    else if (s2CharSubStr.IsAllDigit()) // fixes stuff like 72 (decimal) 48 (hex)
      bytes[i / 2] = Convert.ToByte(s2CharSubStr, 10); // does not convert, so 48 hex stays 48 hex. But will not handle letters.
    else if (s2CharSubStr[0] == '0') // handles things like 10 (decimal) 0A (hex) 
      bytes[i / 2] = // ?????????????????????????????
    else // handle things like AA to FF (hex)
      bytes[i / 2] = // ?????????????????????????????
   }

   return bytes;
 }

像下面两个这样的答案执行从十六进制到十进制的隐式转换(如在 Visual Studio 的 IntelliSense 中所见)和/或无法处理十六进制的 alpha 部分: 1)

bytes[i / 2] = (byte)int.Parse(sSubStr, NumberStyles.AllowHexSpecifier); 

2)

bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16); 

3) bytes[i / 2] = Convert.ToByte(hexIpAddress.Substring(i, 2));

所以我希望函数返回这个硬编码字节数组的等价物:

byte[] currentIpBytes = {0x0A, 01, 02, 0x48};

【问题讨论】:

  • “十进制”和“十六进制值”有什么区别?它们是字节,0x01 与 1 相同,0x0a 与 10 相同,以此类推,当您处理二进制数据而不是字符串数据时,表示与底层数据无关。
  • 但是 2) 是正确的。 byte 值是 byte 值...所以 bytes[0] 的值将是 10(十进制), 0x0A。您还希望bytes[0] 具有什么其他价值?
  • 我希望函数返回这个硬编码字节数组的等价物:byte[] currentIpBytes = {0x0A, 01, 02, 48};
  • 是的,所以bytes[i/2] = Convert.ToByte(hexIpAddress.Substring(i,2)); 完全符合您的要求。 (虽然是0x48,而不是48
  • @Rene, public static byte[] ConvertHexValueToByteArray() { string hexIpAddress = "0A010248"; byte[] bytes = new byte[hexIpAddress.Length / 2]; for (int i = 0; i

标签: c# arrays hex


【解决方案1】:
string hexIpAddress = "0A010248"; // 10.1.2.72 => "0A010248"
byte[] bytes = new byte[hexIpAddress.Length / 2];

for (int i = 0; i < hexIpAddress.Length; i += 2)
    bytes[i/2] = Convert.ToByte(hexIpAddress.Substring(i, 2), 16);

这导致了这个数组:

bytes = {0x0A, 0x01, 0x02, 0x48}; 

或以小数表示:

bytes = {10, 1, 2, 72};

或作为二进制文件000010100000000010000001001001000(C#6 中仍然不支持二进制文字)。

值是相同的byte 中的任何基都没有表示。您只能决定在再次将它们转换为字符串时应该如何表示这些值:

foreach(byte b in bytes)
    Console.WriteLine("{0} {0:X}", b);

结果

10 A
1 1
2 2
72 48

【讨论】:

  • 更正:“hex”在您的答案中必须是“hexIpAddress”。
  • 让我明白。所以我的核心问题是误解了代码洞察显示 {10, 1, 2, 72} 它实际上是 {0x0A, 0x01, 0x02, 0x48} 下的十六进制值?因此,当我将其发送到需要我的 IP 地址为十六进制值的设备时,真的可以吗?
  • @EricWood 很好,这取决于设备以及谁告诉你它“需要十六进制值”,但我很确定它会没事的。顺便说一句:如果您使用的是 Visual Studio,您可以告诉调试器是否应该将值显示为十六进制或十进制(我猜是在“监视”窗口中单击鼠标右键)。
猜你喜欢
  • 2018-01-31
  • 2018-01-22
  • 2019-07-27
  • 2017-08-23
  • 2012-01-24
  • 2017-08-12
  • 1970-01-01
  • 1970-01-01
  • 2012-02-03
相关资源
最近更新 更多