c++:将无符号长整数转换为向量原始数据

时间:2021-03-01 22:29:45

I need to convert a long int x = 0x9c758d0f into a vector<uint8_t> y = 9c 75 8d 0f.

我需要将一个长int x = 0x9c758d0f转换为一个向量 y = 9c758d0f。

I was using:

我用:

std::stringsteam ss;
ss << std::hex << x;

std::string s = ss.str();

std::vector<uint8_t> y;
String2Vector(s,y);

    void String2Vector(std::string& in, std::vector<uint8_t>& output) 
    {
        std::vector<uint8_t> out;
        size_t len = in.length();

        for(size_t i = 0; i < len; i += 1) 
        {
            std::stringstream strm(in.substr(i, 1));
            uint8_t x;
            strm  >>std::hex>> x;
            out.push_back(x);
        }
        output = out;
    }

However, the vector<uint8_t> stored ASCII number instead of hex value.

但是,vector 存储ASCII码,而不是十六进制值。

What should I do in order to convert a long int into a raw data vector? It's a multi-platform project so I don't wanna touch memcpy() etc.

为了将一个长整数转换成一个原始数据向量,我应该怎么做?这是一个多平台项目,所以我不想接触memcpy()等等。


Update: Pretty sure something went wrong:

更新:肯定出了什么问题:

long int x = 0x9c758d0f;
std::vector<uint8_t> v;
v.reserve(sizeof(x));
for (size_t i = 0; i < sizeof(x); ++i) {
    v.push_back(x & 0xFF);
    x = (x>>8);
}
PrintOutVector(v);

void PrintOutVector(std::vector<uint8_t>& in)
    {
        std::cout << "Vector Contains: ";
        for(std::vector<uint8_t>::iterator i=in.begin(); i != in.end(); ++i)
            std::cout << std::hex <<  *i ;
        std::cout << "\n";
    }

the output is ▒C▒▒h4

输出是C▒▒▒h4


Solution: Much thanx to @WhozCraig @Anton Savin

解决方案:感谢@WhozCraig @Anton Savin

long int x = 0x9c758d0f;
std::vector<uint8_t> v;
v.reserve(sizeof(x));
for (size_t i = 0; i < sizeof(x); ++i) {
    v.push_back(x & 0xFF);
    x = (x>>8);
}
PrintOutVector(v);

void PrintOutVector(std::vector<uint8_t>& in)
    {
        std::cout << "Vector Contains: ";
        for(std::vector<uint8_t>::iterator i=in.begin(); i != in.end(); ++i)
            std::cout << std::hex <<  static_cast<unsigned int>(*i)
        std::cout << "\n";
    }

3 个解决方案

#1


1  

Here is a solution which provides same results regardless of byte ordering:

这里有一个解决方案,它提供了相同的结果,而不考虑字节排序:

long int x = 0x9c758d0f;
std::vector<uint8_t> v;
v.reserve(sizeof(x));
for (size_t i = 0; i < sizeof(x); ++i) {
    v.push_back(x & 0xFF);
    x >>= 8;
}

#2


2  

long int x = 0x9c758d0f;
const uint8_t* begin = reinterpret_cast<const uint8_t*>(&x);
const uint8_t* end = begin + sizeof(x);
std::vector<uint8_t> v(begin, end);

Note that the ordering depends on how your system arranges the bytes (big endian or little endian) in the long. You can deal with this by first reordering the bytes to big-endian with a function like htonl(), except that is for int and there is no cross-platform one for long so you'll have to think about what to do there if you care about the byte ordering.

请注意,排序取决于您的系统在长时间内如何排列字节(大字节或小字节)。您可以通过一个类似于htonl()的函数将字节重新排序到big-endian,但是这是针对int的,而且很长一段时间没有跨平台的字节排序,所以如果您关心字节排序的话,您必须考虑在那里做什么。

#3


0  

Unions come in handy for this sort of stuff. Only issue is the byte ordering might vary based on endianness, which you have to account for.

工会在这方面很有用。唯一的问题是字节排序可能会根据endianness不同,这是您必须考虑的。

union U{
    long int i;
    uint8_t uc[4];
};
U u = {0x9c758d0f};
std::vector<uint8_t> ucvec(u.uc, u.uc+4);
printf ("%x:%x:%x:%x\n", ucvec[3], ucvec[2], ucvec[1], ucvec[0]);

#1


1  

Here is a solution which provides same results regardless of byte ordering:

这里有一个解决方案,它提供了相同的结果,而不考虑字节排序:

long int x = 0x9c758d0f;
std::vector<uint8_t> v;
v.reserve(sizeof(x));
for (size_t i = 0; i < sizeof(x); ++i) {
    v.push_back(x & 0xFF);
    x >>= 8;
}

#2


2  

long int x = 0x9c758d0f;
const uint8_t* begin = reinterpret_cast<const uint8_t*>(&x);
const uint8_t* end = begin + sizeof(x);
std::vector<uint8_t> v(begin, end);

Note that the ordering depends on how your system arranges the bytes (big endian or little endian) in the long. You can deal with this by first reordering the bytes to big-endian with a function like htonl(), except that is for int and there is no cross-platform one for long so you'll have to think about what to do there if you care about the byte ordering.

请注意,排序取决于您的系统在长时间内如何排列字节(大字节或小字节)。您可以通过一个类似于htonl()的函数将字节重新排序到big-endian,但是这是针对int的,而且很长一段时间没有跨平台的字节排序,所以如果您关心字节排序的话,您必须考虑在那里做什么。

#3


0  

Unions come in handy for this sort of stuff. Only issue is the byte ordering might vary based on endianness, which you have to account for.

工会在这方面很有用。唯一的问题是字节排序可能会根据endianness不同,这是您必须考虑的。

union U{
    long int i;
    uint8_t uc[4];
};
U u = {0x9c758d0f};
std::vector<uint8_t> ucvec(u.uc, u.uc+4);
printf ("%x:%x:%x:%x\n", ucvec[3], ucvec[2], ucvec[1], ucvec[0]);