Thread: How covert unsigned char[4] to unsigned integer?

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    235

    How covert unsigned char[4] to unsigned integer?

    How to convert 4 bytes n read from binary file to integer header_offset?
    Code:
     uint32_t header_offset ;   char n[4]; size_t in;
      ...
      in = fread(n, 4, 1, fp); // value n is "\000\000\000\020"
      if (!in)
        return -2;
      header_offset = n; // get 4 bytes to integer
    The header_offset is position of bitmap in BMP file.
    Last edited by barracuda; 02-11-2015 at 11:26 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,681
    > How to convert 4 bytes n read from binary file to integer header_offset?
    memcpy perhaps?

    Endianness - Wikipedia, the free encyclopedia
    Though to isolate yourself from endian issues, you need to do something like this
    Code:
    for ( i = 0 ; i < 4 ; i++ ) {
      header_offset = (header_offset << 8) | n[i];
    }
    There is a similar loop for you to work out, if the data in the file happens to be big-endian.
    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.

  3. #3
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    There is no need to invoke any function for this. Using memcpy for 4 bytes? That made my day.
    Code:
    ((unsigned char*)&header_offset)[0] = n[0];
    Will put the value of the first byte of n to the first byte of header_offset.
    Last edited by Al3; 02-11-2015 at 01:15 PM.

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    AI3:
    Thanks! This works perfectly.

    BTW:
    I use this:
    Code:
    unsigned char n[4];
    Last edited by barracuda; 02-11-2015 at 02:59 PM.

  5. #5
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Quote Originally Posted by barracuda View Post
    AI3:
    Thanks! This works perfectly.
    No problem. It is quick and o/f safe way, because unsigned char* is guaranteed to be at least 4 bytes.

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    Yet I decided to place it to function:
    Code:
    static int read4Bytes(FILE * fp, unsigned char** item, unsigned char * n[4])
    {
      size_t in;
      in = fread(n, 4, 1, fp);
      if (!in)
        return -2;
      ((unsigned char*)&item)[0] = n[0];  // get 4 bytes string to integer
    }
    I plan to use like this:
    Code:
    unsigned char n[4];
    read4Bytes(fp, &header_offset, &n);
    read4Bytes(fp, &result->dib_header_size, &n);
    But I want to ask you how to declare argument 3 in read4Bytes()?

  7. #7
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Quote Originally Posted by barracuda View Post
    Yet I decided to place it to function:
    Code:
    static int read4Bytes(FILE * fp, unsigned char** item, unsigned char * n[4])
    {
      size_t in;
      in = fread(n, 4, 1, fp);
      if (!in)
        return -2;
      ((unsigned char*)&item)[0] = n[0];  // get 4 bytes string to integer
    }
    I plan to use like this:
    Code:
    unsigned char n[4];
    read4Bytes(fp, &header_offset, &n);
    read4Bytes(fp, &result->dib_header_size, &n);
    But I want to ask you how to declare argument 3 in read4Bytes()?
    I said it before, I will say it again:
    Code:
    ((unsigned char*)&item)[0] = n[0];
    Only assigns n[0] to the first byte of item. Item is 4 bytes, which means it works as long as the value don't go above 1 byte (is maximum 255).

    You can do
    Code:
    ((unsigned char*)&item)[1] = n[1]
    for the rest of the bytes of course. What do you mean by "But I want to ask you how to declare argument 3 in read4Bytes()?" Also why do you reference to n? n is already an address in your exemplification.
    Last edited by Al3; 02-11-2015 at 04:02 PM.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I would still use the loop unless you're sure that the code will only run and the files come from machines with the same endianness. Al3 is happy to ignore it, I guess, because he doesn't port code and always works on the same architecture.

    unsigned char * n[4] is an array of 4 unsigned char pointers. you don't need the subscript. This means you're calling read4bytes wrong too.
    read4bytes(fp, &result->dib_header, n);

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    If you don't care about endianess, you can do:
    Code:
    header_offset = *((uint32_t*)&n)
    or, even more straightforward, bypassing all those unnecessary steps:
    Code:
    in = fread(&header_offset, 4, 1, fp);
    Devoted my life to programming...

  10. #10
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    I got some strange results:

    http://oi61.tinypic.com/k351dj.jpg

    Code:
    static int read2Bytes(FILE * fp, uint16_t* item, unsigned char * n)
    {
      size_t in;
      in = fread(n, 2, 1, fp);
      if (!in) return -2;
      ((unsigned char*)&item)[0] = n[0];
      ((unsigned char*)&item)[1] = n[1];
      return in;
    }
    static int read4Bytes(FILE * fp, uint32_t* item, unsigned char * n)
    {
      size_t in;
      in = fread(n, 4, 1, fp);
      if (!in) return -2;
      ((unsigned char*)&item)[0] = n[0];
      ((unsigned char*)&item)[1] = n[1];
      ((unsigned char*)&item)[2] = n[2];
      ((unsigned char*)&item)[3] = n[3];
      return in;
    }
    PS:
    Sorry, I have slow reactions to read back your posts
    Last edited by barracuda; 02-11-2015 at 04:33 PM.

  11. #11
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Ok, now, backstep for a bit. Take a look at the following line of code and explain to me what it does, would you?
    Code:
    ((unsigned char*)&item)[0] = n[0];
    EDIT: Oh, I see you noticed it.
    Last edited by GReaper; 02-11-2015 at 04:51 PM.
    Devoted my life to programming...

  12. #12
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    I understand that this
    ((unsigned char*)&item)[1] is reference to the second byte of the string. It should copy byte by byte to the referenced item.

  13. #13
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    Wait, why there is type unsigned char* and not uint32_t ?

  14. #14
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Quote Originally Posted by barracuda View Post
    Wait, why there is type unsigned char* and not uint32_t ?
    I know, right?! That's what I said!
    But it would need to be written kinda backwards...
    Devoted my life to programming...

  15. #15
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Quote Originally Posted by GReaper View Post
    I know, right?! That's what I said!
    But it would need to be written kinda backwards...
    Only if his system is B/E.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Howto extract a positive integer from an unsigned char array
    By GregoireLeGros in forum C++ Programming
    Replies: 13
    Last Post: 01-14-2015, 02:14 AM
  2. Unsigned char array to unsigned char?
    By Kishintai in forum C Programming
    Replies: 16
    Last Post: 04-26-2013, 01:37 PM
  3. Replies: 2
    Last Post: 10-06-2009, 09:37 AM
  4. Converting unsigned long array to unsigned char array
    By delvec28 in forum C Programming
    Replies: 2
    Last Post: 09-07-2009, 08:53 PM
  5. convert from an integer to an unsigned char
    By Landroid in forum C Programming
    Replies: 4
    Last Post: 05-02-2005, 01:43 AM