C++:将 unsigned long long int 转换为向量反之亦然

发布于 2024-12-29 02:28:52 字数 458 浏览 2 评论 0原文

谁能告诉我如何将 unsigned long long int 转换为向量,反之亦然。

为了从 unsigned long long int 转换为向量,我尝试了以下操作:

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

当我测试 x = 1234567890 时,它失败了。但是当我尝试使用较小的 x 值(例如 1-100)时,它起作用了......

为了将向量转换为 unsigned long long int,我使用了:

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

谁能告诉我该怎么做。

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...

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贱人配狗天长地久 2025-01-05 02:28:52

请记住,复制字节不能跨平台移植。您的 memcpy 看起来不错,那么为什么不在返回时重新创建它呢?您编写的内容只是获取向量的第一个字节并将其转换为 unsigned long long 这解释了为什么它适用于小数字。

尝试以下方法从向量中获取值:

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

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.

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

unsigned long long int x;
memcpy(&x, &buf[0], sizeof(x));
沙与沫 2025-01-05 02:28:52

您可以使用 std::vector::assign 来执行复制,而不是直接 memcpy 到向量中。

#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;
}

输出:

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

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

#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
情域 2025-01-05 02:28:52

你必须改变你的最后一行:

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

编辑我在胡说八道,那行本来就很好,但是你的转换回来了很糟糕 - 你转换了值,而不是指针:

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

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]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文