c++:将无符号长整数转换为向量,反之亦然

时间:2021-09-04 01:10:03

Can anyone tell me how to convert unsigned long long int into vector and vice versa.

谁能告诉我如何将无符号长整数转换成向量,反之亦然。

For converting from unsigned long long int to vector, I tried the following:

对于从无符号长整数到向量的转换,我尝试了以下方法:

unsigned long long int x;
vector<char> buf(sizeof(x));
memcpy( &buf[0], &x, sizeof( x ) );

When I tested for x = 1234567890, it failed. But when I tried it for smaller values of x (say 1-100), it works...

当我测试x = 1234567890时,它失败了。但是当我用x的值(比如1-100)更小的时候,它是有效的。

For converting vector to unsigned long long int, I used:

对于将向量转换为无符号长整数,我使用:

   unsigned long long int =  (unsigned long long int)buf[0];

Can anyone tell me how to do it.

谁能告诉我怎么做吗?

3 个解决方案

#1


7  

Just remember that copying bytes around won't be cross-platform portable. Your memcpy looks fine, so why not re-create that on the way back out? What you've written simply takes the first byte of the vector and converts it to an unsigned long long which explains why it works for small numbers.

只要记住,到处复制字节不会是跨平台的可移植性。你的memcpy看起来不错,那么为什么不在返回时重新创建它呢?您所写的只是获取向量的第一个字节并将其转换为无符号的长长,这就解释了为什么它适用于小数字。

Try this instead to get the value back out of the vector:

试着从向量中得到值:

unsigned long long int x;
memcpy(&x, &buf[0], sizeof(x));

#2


4  

Instead of memcpying directly into the vector you can use std::vector::assign to perform the copying.

可以使用std:::vector::assign来执行复制,而不是直接将memcpying转换为vector。

#include <iostream>
#include <vector>

int main()
{
  unsigned long long int x = 0x0807060504030201;
  std::vector<char> v;

  v.assign( reinterpret_cast<char *>( &x ), reinterpret_cast<char *>( &x ) + sizeof( x ) );

  for( auto i = v.begin(); i != v.end(); ++i ) {
    std::cout << std::hex << static_cast<int>( *i ) << ' ';
  }
  std::cout << std::endl;

  // To convert back
  auto y = *reinterpret_cast<unsigned long long int *>( &v[0] );
  std::cout << "y = " << std::hex << std::showbase << y << std::endl;

  return 0;
}

Output:

输出:

1 2 3 4 5 6 7 8 
y = 0x807060504030201

#3


3  

You have to change your last line:

你必须改变你的最后一句话:

memcpy( &buf[0], reinterpret_cast<char*>(&x), sizeof( x ) );

Edit I was talking nonsense, that line is fine the way it is, but your conversion back is bad - you convert the value, instead of the pointer:

编辑:我说的是废话,这行是可以的,但你的转换回来是不好的-你转换值,而不是指针:

unsigned long long int val = *reinterpret_cast<unsigned long long int*>(&buf[0]);

#1


7  

Just remember that copying bytes around won't be cross-platform portable. Your memcpy looks fine, so why not re-create that on the way back out? What you've written simply takes the first byte of the vector and converts it to an unsigned long long which explains why it works for small numbers.

只要记住,到处复制字节不会是跨平台的可移植性。你的memcpy看起来不错,那么为什么不在返回时重新创建它呢?您所写的只是获取向量的第一个字节并将其转换为无符号的长长,这就解释了为什么它适用于小数字。

Try this instead to get the value back out of the vector:

试着从向量中得到值:

unsigned long long int x;
memcpy(&x, &buf[0], sizeof(x));

#2


4  

Instead of memcpying directly into the vector you can use std::vector::assign to perform the copying.

可以使用std:::vector::assign来执行复制,而不是直接将memcpying转换为vector。

#include <iostream>
#include <vector>

int main()
{
  unsigned long long int x = 0x0807060504030201;
  std::vector<char> v;

  v.assign( reinterpret_cast<char *>( &x ), reinterpret_cast<char *>( &x ) + sizeof( x ) );

  for( auto i = v.begin(); i != v.end(); ++i ) {
    std::cout << std::hex << static_cast<int>( *i ) << ' ';
  }
  std::cout << std::endl;

  // To convert back
  auto y = *reinterpret_cast<unsigned long long int *>( &v[0] );
  std::cout << "y = " << std::hex << std::showbase << y << std::endl;

  return 0;
}

Output:

输出:

1 2 3 4 5 6 7 8 
y = 0x807060504030201

#3


3  

You have to change your last line:

你必须改变你的最后一句话:

memcpy( &buf[0], reinterpret_cast<char*>(&x), sizeof( x ) );

Edit I was talking nonsense, that line is fine the way it is, but your conversion back is bad - you convert the value, instead of the pointer:

编辑:我说的是废话,这行是可以的,但你的转换回来是不好的-你转换值,而不是指针:

unsigned long long int val = *reinterpret_cast<unsigned long long int*>(&buf[0]);