Alex Rivera | Logout

Converting a C byte array to a long long

Asked 2011-10-24T18:16:11.307
8

I have an 8-byte array in my application with this data:

00000133339e36a2

This data represents a long (on the platform the data was written in, on a Mac this would be a long long) with the value of

1319420966562

In the actual application this is a semi-randomized set of data, so the number will always be different. Therefore, I need to convert the byte array into a printable long long.

I've tried casting the data directly into a long long, but I came up with

1305392

where I should have been seeing the above number.

For those of you with more experience in C byte manipulation than I do, how would I correctly convert a byte array to a long long?

EDIT: Strangely, all of your solutions keep outputting the same number: 866006690. That is the decimal equivalent of the last four bytes of the data.

Edit
Report

1 Answer

1

Great question! And great answers! To throw in a cent:

// Getting stuff "in"
unsigned long long ll1 = 1319420966562;
unsigned char * c = reinterpret_cast<unsigned char *>( &ll1 );

// Getting stuff "out"
unsigned long long ll2;
memcpy( &ll2, c, 8 );

ll2 is now equal to ll1.

I must admin, however, I like the union examples better. :)

answered 2011-10-24T18:58:02.220

Your Answer