Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a C byte array to a long long

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.

like image 494
voidzm Avatar asked Oct 24 '11 18:10

voidzm


Video Answer


1 Answers

This seems one of the (rare) situations where an union is useful:

union number
{
    char charNum[16];
    long long longNum;
};
like image 162
BlackBear Avatar answered Sep 29 '22 19:09

BlackBear