Is there a way to convert 8-bytes stored in an array into long representation?
Printable View
Is there a way to convert 8-bytes stored in an array into long representation?
Yes.
Quzah.
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.
i tried that approach but the screwy lcc compiler wouldnt work right
How about posting the code you tried?
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.Quote:
Originally Posted by Salem
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.
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.
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'
...
...