如何将二进制 IPv6 地址转换为十六进制

How to convert a binary IPv6 address into hexadecimal

本文关键字:转换 十六进制 地址 IPv6 二进制      更新时间:2023-10-16

我收到一个 128 位二进制地址,我需要将其转换为十六进制格式。这是我必须做的:

  • 我将收到 128 位二进制数
  • 我需要将其分成 8 个块,每个块 16 位,
  • 所以我将有 8 个变量,每个块都有 16 位。

    0010000111011010 0000000011010011 000 0010111100111011 0000001010101010 0000000011111111 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

  • 现在,我需要进一步将这 16 位分成 4 位,即一个半字节

    0010

    0001 1101 1010
    0000 0000 1101 0011

  • 0000 0000 0000 0000
  • 所以,现在我将为每个变量获得四个子变量

  • 每个变量的长度为 16 位,子变量的长度为 4 位
  • 比,我需要将这些子变量中的每一个转换为十六进制

    0010

    0001 1101 1010

    2 1 天安

  • 组合所有子变量并以十六进制形式形成 IPv6 地址:

    21DA:00D3:0000:

    2F3B:02AA:00FF:0000:0000

谁能告诉我如何在C++做到这一点?

要获得 16 位值的四个最高位,请将其向右移动 12 位。这将导致顶部位为零,因此无需屏蔽。对于其他人,您(可选)向右移动并使用按位和运算符&屏蔽掉四个最低位。

要使从上述步骤获得的值变为字符形式的十六进制数字,则对于小于 10 的值,请添加'0'(如果您在使用 ASCII 编码的计算机上),对于 10 或更高的值,则减去 10 并添加例如 'A' .


当然,也有更简单的方法,例如使用 sprintf直接转换数字。只需将 16 位值放入无符号短整型中,然后执行例如

printf("%04hxn", value_as_unsigned_short);

假设您有二进制数0001001010101011。这是,在十六进制表示12ab.

如果二进制数在整数变量中,假设一个名为 value 的变量,我们可以得到十六进制表示为字符串,如下所示:

// First get each digit, one by one
digit0 = value & 0x0f;  // Mask out everything but the lowest four bits
digit1 = (value >>  4) 0x0f;
digit2 = (value >>  8) 0x0f;
digit3 = value >> 12;
// Now make a string out of those four digits
char str[5];  // Four digits plus the string terminator
// If a digit is less than 10, then add '0'  to get an ASCII character
// Else decrease by ten (to get a number between 0 and 5) and add 'A'
str[0] = digit3 < 10 ? digit3 + '0' : (digit3 - 10) + 'A';
str[1] = digit2 < 10 ? digit2 + '0' : (digit2 - 10) + 'A';
str[2] = digit1 < 10 ? digit1 + '0' : (digit1 - 10) + 'A';
str[3] = digit0 < 10 ? digit0 + '0' : (digit0 - 10) + 'A';
str[4] = ''; // Terminate string
printf("value is in hex %sn", str);

上面的代码将打印

值以十六进制 12AB 为单位

然而,这是很多代码,尽管它可以重用于所有数字。如果你已经在整数变量 value 中有 16 位数字,那么写起来要容易得多

printf("value is in hex %04hXn", value);

上面两个代码片段的结果将是相同的。


关于您的编辑:

std::ostringstream oss;
for (size_t i = 0; i < 8; ++i, aBinaryIPAddress += 2)
{
    // Take the first byte, and shift it left 8 bits, making it
    // the high byte of a 16-bit number. Then or (or add) the next
    // byte at the low 8 bits in the 16-bit number.
    // The casting is needed because we're working with 16-bit numbers
    // and not bytes.
    uint16_t value = static_cast<uint16_t>(*aBinaryIPAddress << 8) |
                     static_cast<uint16_t>(*(aBinaryIPAddress + 1));
    oss << std::setfill('0') << std::setw(4) << std::hex << value;
    if (i < 7)
        oss << ':';
}
std::cout << "Address is " << oss.str() << 'n';

您可以使用 sprintf 和格式说明符 X 将整数值打印为十六进制。无需将块划分为每个超过 4 个字节。所以如果你有这个:

string a = "00100001110110100000000011010011";
unsigned value = 0;
for (int i  = 0;i < a.size();++i) {
  value = value * 2 + a[i] - '0';
}
printf("%04Xn", value);

将解决您问题的很大一部分。我已经在上面使用了printf来演示标准输出的输出。