C++ 将整数转换为4字节无符号字符向量(以大端字节顺序)

C++ 将整数转换为4字节无符号字符向量(以大端字节顺序),c++,C++,假设,我想在一个二进制文件中写入十进制31,这个二进制文件已经以4字节的形式加载到向量中,所以我必须以00 1f的形式写入,但我不知道如何将十进制数转换为4字节的十六进制字符串 因此,无符号字符向量中的十六进制数应为: 0x00 0x00 0x00 0x1f // int value of this is 31 为此,我尝试了以下方法: std::stringstream stream; stream << std::setfill('0') << std::setw(

假设,我想在一个二进制文件中写入十进制31,这个二进制文件已经以4字节的形式加载到向量中,所以我必须以00 1f的形式写入,但我不知道如何将十进制数转换为4字节的十六进制字符串

因此,无符号字符向量中的十六进制数应为:

0x00 0x00 0x00 0x1f // int value of this is 31
为此,我尝试了以下方法:

std::stringstream stream;
stream << std::setfill('0') << std::setw(sizeof(int) * 2) << std::hex << 31;
cout << stream.str();
上面的代码行以字符串格式提供输出,但我希望它以“0x”格式转换为无符号字符的向量,因此我的输出向量在转换后应具有0x00 0x00 0x00 0x1F的元素。

无需麻烦,您可以将int值复制到适当大小的字符缓冲区中。这个缓冲区可以是向量本身

也许是这样的:

std::vector<uint8_t> int_to_vector(unsigned value)
{
    // Create a vector of unsigned characters (bytes on a byte-oriented platform)
    // The size will be set to the same size as the value type
    std::vector<uint8_t> buffer(sizeof value);

    // Do a byte-wise copy of the value into the vector data
    std::memcpy(buffer.data(), &value, sizeof value);

    return buffer;
}
std::vector<uint8_t> int_to_be_vector(unsigned value)
{
    // Create a vector of unsigned characters (bytes on a byte-oriented platform)
    // The size will be set to the same size as the value type
    std::vector<uint8_t> buffer(sizeof value);

    // For each byte in the multi-byte value, copy it to the "correct" place in the vector
    for (size_t i = buffer.size(); i > 0; --i)
    {
        // The cast truncates the value, dropping all but the lowest eight bits
        buffer[i - 1] = static_cast<uint8_t>(value);
        value >>= 8;
    }

    return buffer;
}
不必麻烦,您可以将int值复制到适当大小的字符缓冲区中。这个缓冲区可以是向量本身

也许是这样的:

std::vector<uint8_t> int_to_vector(unsigned value)
{
    // Create a vector of unsigned characters (bytes on a byte-oriented platform)
    // The size will be set to the same size as the value type
    std::vector<uint8_t> buffer(sizeof value);

    // Do a byte-wise copy of the value into the vector data
    std::memcpy(buffer.data(), &value, sizeof value);

    return buffer;
}
std::vector<uint8_t> int_to_be_vector(unsigned value)
{
    // Create a vector of unsigned characters (bytes on a byte-oriented platform)
    // The size will be set to the same size as the value type
    std::vector<uint8_t> buffer(sizeof value);

    // For each byte in the multi-byte value, copy it to the "correct" place in the vector
    for (size_t i = buffer.size(); i > 0; --i)
    {
        // The cast truncates the value, dropping all but the lowest eight bits
        buffer[i - 1] = static_cast<uint8_t>(value);
        value >>= 8;
    }

    return buffer;
}

您可以使用循环每次提取原始数字的一个字节,并将其存储在向量中

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <vector>

using u8 = std::uint8_t;
using u32 = std::uint32_t;

std::vector<u8> GetBytes(const u32 number) {
    const u32 mask{0xFF};
    u32 remaining{number};

    std::vector<u8> result{};
    while (remaining != 0u) {
        const u32 bits{remaining & mask};
        const u8 res{static_cast<u8>(bits)};
        result.push_back(res);
        remaining >>= 8u;
    }
    std::reverse(std::begin(result), std::end(result));
    return result;
}

int main() {
    const u32 myNumber{0xABC123};
    const auto bytes{GetBytes(myNumber)};
    std::cout << std::hex << std::showbase;
    for (const auto b : bytes) {
        std::cout << static_cast<u32>(b) << ' ';
    }
    std::cout << std::endl;
    return 0;
}

您可以使用循环每次提取原始数字的一个字节,并将其存储在向量中

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <vector>

using u8 = std::uint8_t;
using u32 = std::uint32_t;

std::vector<u8> GetBytes(const u32 number) {
    const u32 mask{0xFF};
    u32 remaining{number};

    std::vector<u8> result{};
    while (remaining != 0u) {
        const u32 bits{remaining & mask};
        const u8 res{static_cast<u8>(bits)};
        result.push_back(res);
        remaining >>= 8u;
    }
    std::reverse(std::begin(result), std::end(result));
    return result;
}

int main() {
    const u32 myNumber{0xABC123};
    const auto bytes{GetBytes(myNumber)};
    std::cout << std::hex << std::showbase;
    for (const auto b : bytes) {
        std::cout << static_cast<u32>(b) << ' ';
    }
    std::cout << std::endl;
    return 0;
}

你有什么问题?像这样格式化它或获取向量中的值?@Timo,我无法获取unsigned char的向量中的值。sizeofint可能不会像您认为的那样做。在今天所有的普通平台上都是4,所以你可以设置为8。@hyde,这正是OP想要的。一个字节取两个十六进制数字。所以,如果你想用十六进制显示一个int,你需要sizeofint*2位数字。实际上,假设我想用4个字节在一个二进制文件中写入十进制31。如果在二进制流中写入一个整数,则会发生这种情况。没有必要转换任何给定正确endianess的内容。您有什么问题?像这样格式化它或获取向量中的值?@Timo,我无法获取unsigned char的向量中的值。sizeofint可能不会像您认为的那样做。在今天所有的普通平台上都是4,所以你可以设置为8。@hyde,这正是OP想要的。一个字节取两个十六进制数字。所以,如果你想用十六进制显示一个int,你需要sizeofint*2位数字。实际上,假设我想用4个字节在一个二进制文件中写入十进制31。如果在二进制流中写入一个整数,则会发生这种情况。没有必要将给定正确的endianess的任何内容进行转换。我们是否也可以直接将memcpy转换为vector::data,因为vector会相应地调整大小?@Timo我不确定,考虑到严格的别名等等。实际上,我不确定临时数组是否可以是uint8_t,或者它是否必须是char。@Someprogrammerdude和@Timo实际上是一个t*,因此对于astd::vector来说,它应该是安全的,没有严格的别名冲突,对吗?@正如前面提到的,我的函数不关心尾数问题。更改endianness的一个简单方法是简单地反转向量。@一个错误,他想说这个函数将始终使用系统/平台endianness,因此如果系统是little endian,则使用little endian,否则使用big endian。如果你想改变它,你可以像前面提到的那样反转向量。如果向量被相应地调整大小,我们不能直接将memcpy转换成vector::data吗?@Timo我不确定,考虑到严格的别名等等。实际上,我不确定临时数组是否可以是uint8_t,或者它是否必须是char。@Someprogrammerdude和@Timo实际上是一个t*,因此对于astd::vector来说,它应该是安全的,没有严格的别名冲突,对吗?@正如前面提到的,我的函数不关心尾数问题。更改endianness的一个简单方法是简单地反转向量。@一个错误,他想说这个函数将始终使用系统/平台endianness,因此如果系统是little endian,则使用little endian,否则使用big endian。如果要更改它,可以按照前面提到的反转向量。