I have an array of 8 bytes and I'm trying to convert it to a signed long in C++, and can't seem to figure it out. From what I could tell long ints are only 4 bytes, can anybody provide some information on this? Is it going to matter if it is 32 or 64 bit?
You probably should use a int64_t which is guaranteeed to be 8 bytes long.
You don't state how your data is represented (its endianness) into your array but you might use reinterpret_cast<> or even better: use shift operations to "build" your integer.
Something like:
unsigned char array[8] = { /* Some values here */ };
uint64_t value =
static_cast<uint64_t>(array[0]) |
static_cast<uint64_t>(array[1]) << 8 |
static_cast<uint64_t>(array[2]) << 16 |
static_cast<uint64_t>(array[3]) << 24 |
static_cast<uint64_t>(array[4]) << 32 |
static_cast<uint64_t>(array[5]) << 40 |
static_cast<uint64_t>(array[6]) << 48 |
static_cast<uint64_t>(array[7]) << 56;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With