Is there a way to convert 8-bytes stored in an array into long representation?
Is there a way to convert 8-bytes stored in an array into long representation?
Yes.
Quzah.
Hope is the first step on the road to disappointment.
hmm, can you give me hints, like header files, to look into? Thanks.
Well how would you extract bytes out of an unsigned long?
Use bit masks and bit shifts.
Packing them in is just the reverse.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
i tried that approach but the screwy lcc compiler wouldnt work right
How about posting the code you tried?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
If your 8 bytes are contiguous within the array then a simple cast might work if you just want to reinterpret those 8 bytes, or a memcpy from those 8 bytes into the memory occupied by an 8-byte long value could do the job.
Seconded.Originally Posted by Salem
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
Code:#include <stdio.h> int main(int argc, char *argv[]) { unsigned char bt[4] = {1,4,5,3}; unsigned long num1=0; num1 = (0xFFFFFFFF & bt[0]) << 24; num1 = num1 |(0xFFFFFFFF & bt[1]) << 16; num1 = num1 | (0xFFFFFFFF & bt[2]) << 8; num1 = num1 |(0xFFFFFFFF & bt[3]); printf ("\nNum1: %lu\n",num1); return 0; }
Also, consider this:
Code:#include <stdio.h> int main( void ) { printf("The size of a long is %u. The sizeof your array is 8.\n", sizeof( long ) ); return 0; }
Quzah.
Hope is the first step on the road to disappointment.
Code:for (i = begin_index; i < begin_index + 8; i++) { x = (uint64_t) str[i]; printf("%d,", x); (x >= 0) ? x : x + 256; for (j = 0; j < y; j++) x = x << 8; tile_seed = tile_seed | x; y--; }
However, you should know that this will make the left most value in the string the least significant byte. (As yours would if it worked.)Code:for( i = 0; i < 8; i++ ) x |= (uint64_t)( str[ i ] << ( i << 3 ) );
Quzah.
Hope is the first step on the road to disappointment.
Looks like it could just be compiler issues, so I switched to GCC Instead. Not sure if this should go here, or networking, but my winsock2 functions arent being referenced? properly. Not sure how to get rid of the following:
c.text+0x300d): undefined reference to `_itoa'
c.text+0x32db): undefined reference to `_send@16'
c.text+0x3367): undefined reference to `_send@16'
...
...