Converting Bytes to Numbers

This is a discussion on Converting Bytes to Numbers within the C Programming forums, part of the General Programming Boards category; I am reading 4 individual bytes via a serial port. The 4 bytes combined represent a number in decimal. How ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    Converting Bytes to Numbers

    I am reading 4 individual bytes via a serial port. The 4 bytes combined represent a number in decimal. How do I calculate the number in code? I can do it on paper...

    For example, I read the following 4 bytes.

    0x00 (0)
    0x00 (0)
    0x0E (14)
    0x1C (28)

    In reality this represents the number 0x00000E1C (or 3612).

    I know that. How do I code the computer to calculate that for me other than a long drawn out seperation of the hex digits using strings etc.?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Some bitwise operations would make sense, though you could also use multiplication and addition:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        unsigned char bytes[] = {0x00, 0x00, 0x0E, 0x1C};
        unsigned int number = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
        printf("%u\n", number);
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Wow that looks simple... I had considered using the bitwise operators but always thoughif you did << too many times you get 0 because you push the value bits outside the space...

    1111 << 1 => 1110
    1111 << 2 => 1100 etc...

    I guess I was mistaken...

    Thanks laserlight.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Quote Originally Posted by Bladactania
    I had considered using the bitwise operators but always thoughif you did << too many times you get 0 because you push the value bits outside the space...
    Speaking of that, I made a reasonable assumption by writing 24, 16 and 8. It may be clearer and somewhat more portable (but I doubt that that is a concern here) to change those magic numbers to (3 * CHAR_BIT), (2 * CHAR_BIT) and CHAR_BIT respectively.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by laserlight View Post
    Speaking of that, I made a reasonable assumption by writing 24, 16 and 8. It may be clearer and somewhat more portable (but I doubt that that is a concern here) to change those magic numbers to (3 * CHAR_BIT), (2 * CHAR_BIT) and CHAR_BIT respectively.
    I kind of disagree... The serial port's concept of a "byte" doesn't necessarily relate to the CPU's concept. Presumably you should already know the bitness of a byte coming from the serial port, and use that. So you could still use a macro, but CHAR_BIT is probably not the right one. Maybe a locally defined BITS_PER_BYTE or something like that.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Yeah I gathered that... in this case it doesn't matter, but since I'll be using this code elsewhere I've added that...

    And it works great... Thanks!

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Quote Originally Posted by brewbuck
    Maybe a locally defined BITS_PER_BYTE or something like that.
    Yeah, that would be good too, the point being to replace the magic numbers with something readable.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by laserlight View Post
    Yeah, that would be good too, the point being to replace the magic numbers with something readable.
    Of course, anybody accustomed to this kind of code would immediately understand what those values mean...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    YEah, but it's those who aren't accustomed to this kind of code that you have to allow for.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bladactania View Post
    YEah, but it's those who aren't accustomed to this kind of code that you have to allow for.
    They shouldn't mess with that code!

    Seriously, you have to accept some sort of minimal expected experience.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting numbers to words
    By dionys in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 09:34 AM
  2. converting a string of numbers into an integer value -How?
    By v3ct0r_sect0r in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2004, 07:58 AM
  3. converting numbers to letters??
    By Lau in forum C Programming
    Replies: 5
    Last Post: 11-06-2002, 11:17 AM
  4. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

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