Thread: Converting an 8-Byte Array into a Long Representation

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    54

    Converting an 8-Byte Array into a Long Representation

    Is there a way to convert 8-bytes stored in an array into long representation?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    54
    hmm, can you give me hints, like header files, to look into? Thanks.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    54
    i tried that approach but the screwy lcc compiler wouldnt work right

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.

    Quote Originally Posted by Salem
    How about posting the code you tried?
    Seconded.
    "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

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    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;
    }

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    54
    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--;
    	}

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for( i = 0; i < 8; i++ )
        x |= (uint64_t)( str[ i ] << ( i << 3 ) );
    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.)


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Feb 2005
    Posts
    54
    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'
    ...
    ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Byte Array problem
    By OldGit in forum Networking/Device Communication
    Replies: 8
    Last Post: 02-20-2009, 05:45 AM
  2. sorting using pointer to pointer not working
    By eager2no in forum C Programming
    Replies: 17
    Last Post: 09-21-2008, 12:52 AM
  3. Quick, Partiotion, and Insertion Sort
    By silicon in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2005, 08:47 PM
  4. Merge and Heap..which is really faster
    By silicon in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 04:06 PM
  5. Converting byte arrays to vectors
    By kasun in forum C++ Programming
    Replies: 3
    Last Post: 03-02-2004, 10:31 AM