4

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.

veda
  • 6,416
  • 15
  • 58
  • 78
  • yes, I am trying to split the integer into a vector so that i can send it from the client to the server and read it back. – veda Jan 25 '12 at 16:01
  • If you want to do that, you should probably consider combining Mark B's solution into template functions which can write/read any sized type into your vector safely. – Benj Jan 25 '12 at 16:15

3 Answers3

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.

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

unsigned long long int x;
memcpy(&x, &buf[0], sizeof(x));
Mark B
  • 95,107
  • 10
  • 109
  • 188
  • 2
    Hehe, and if you tried his current approach on a big endian system buf[0] would only contain anything if you had a really big value of x. – Benj Jan 25 '12 at 16:04
4

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
Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • How to save it in reversed order, I mean the output of printing will be 8 7 6 5 4 3 2 1? – rank1 May 08 '13 at 16:19
  • 1
    @cygi1989 Use [`std::reverse`](http://en.cppreference.com/w/cpp/algorithm/reverse). After `v.assign(...);` add `std::reverse(v.begin(), v.end());` – Praetorian May 08 '13 at 16:25
  • Great. Is it possible to assign it smallest possible vector, let's say I have a function which return the smalles number of bytes necessary to store given integer. How would I do it? – rank1 May 08 '13 at 16:41
  • @cygi1989 I'm not sure what you're asking. Why don't you post another question? – Praetorian May 08 '13 at 17:12
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]);
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283