Conver long to 8 byte array and back to long

This is a discussion on Conver long to 8 byte array and back to long within the C Programming forums, part of the General Programming Boards category; I have these two functions to convert one long to a 8 byte array and to convert a byte array ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    1

    Conver long to 8 byte array and back to long

    I have these two functions to convert one long to a 8 byte array and to convert a byte array to a long, but somethings isn't right..
    I think that is my shifts, but I'm not certain.


    Code:
    unsigned char* longToByteArray(long value){
      unsigned char *b;
      b=(unsigned char*)malloc(sizeof(unsigned char)*8);
      int i;
      for(i = 0; i < 8;i++){
        int offset = (8 - 1 - i) * 8;
        b[i] = (unsigned char) ((value  >> offset) & 0xFFFFFFFL);
    
       
      }
      return b;
    }
    Code:
    long byteArrayToLong(unsigned char* b, int offset) {
      long value = 0;
      int i;
      for (i = 0; i <8; i++) {
        int shift = (8 - 1 - i) * 8;
        value += (b[i + offset] & 0xFFFFFFFL) << shift  ;
      }
      return value;
    
    }
    When I convert long x =1111, the bytes are
    b[0]=0x0, b[1]=0x0, b[2]=0x4, b[3]=0x57,
    b[4]=0x0, b[5]=0x0, b[6]=0x4, b[7]=0x57
    and should be:
    b[0]=0x0, b[1]=0x0, b[2]=0x0, b[3]=0x0,
    b[4]=0x0, b[5]=0x0, b[6]=0x4, b[7]=0x57

    If someone can help me, I would be grateful.
    Thanks in advance.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Be lazy and use a union:
    Code:
        union bl {
            unsigned char b[ sizeof( long )];
            long l;
        };
    Are you sure your long is eight bytes?


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

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    Whether it really is 8 bytes or not, all of those 8's should be sizeof(long)'s.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    and mask should be 0xFF

    why do you need mask when converting back from array to long?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. Quick, Partiotion, and Insertion Sort
    By silicon in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2005, 08:47 PM
  3. Merge and Heap..which is really faster
    By silicon in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 04:06 PM
  4. Insertion Sort Problem
    By silicon in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2005, 12:30 PM
  5. Replies: 3
    Last Post: 04-02-2002, 12:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21