2

I need to convert for exemple this number : 281474976710655 to a unsigned char array like this one :

unsigned char value[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };

I try to memcpy and other techniques but i didn't get the correct value back, always random hex...

I'm not a c++ developer so thanks to explain if you can.

Thanks a lot, bye !

Raphaël Dev
  • 63
  • 2
  • 11
  • What values did you expect, and what did you get? And 6 bytes doesn't seem to be the proper length for a 64-bit value. – Bo Persson Apr 04 '18 at 09:56
  • Possible duplicate of [C - unsigned int to unsigned char array conversion](https://stackoverflow.com/questions/10319805/c-unsigned-int-to-unsigned-char-array-conversion) – xskxzr Apr 04 '18 at 10:59
  • 1
    Remember to bear in mind that if you use something like memcpy then the byte order will depend on endianness... – Sean Burton Apr 04 '18 at 12:13

1 Answers1

10
uint64_t x=281474976710655
unsigned char value[sizeof(x)];
std::memcpy(value,&x,sizeof(x));
Oliv
  • 17,610
  • 1
  • 29
  • 72