Please help hex bytes to word to dec...

This is a discussion on Please help hex bytes to word to dec... within the C Programming forums, part of the General Programming Boards category; Hi all, Really hoping someone is willing to help a newbie here ) Right the problem i have is I've ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    4

    Question Please help hex bytes to word to dec...

    Hi all,
    Really hoping someone is willing to help a newbie here )

    Right the problem i have is I've wrote a program that works out some bytes of hex.(checksum)

    I get these bytes in an array resulting from a loop
    so i have byte[0] byte[1] byte[2] byte[3]

    fine no problems but i need to 'join' all these bytes and display the result in decimal.

    eg
    byte[0]=170
    byte[1]=200
    byte[2]=50
    byte[3]=155
    printf("%X %X %X %X2", byte[1],byte[2],byte[3],byte[4]);

    i can display like AA C8 32 9B

    But i need the bytes in a word AAC8329B so i can change to decimal (2865246875)


    How to change hex bytes into a word ???

    Thanks for your time and sorry about such a basic question.... 'fraid i'm still scratching my head and learning

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Code:
    {
      unsigned long word;
    
      word = byte[3];
      word |= byte[2] << 8;
      word |= byte[1] << 16;
      word |= byte[0] << 24;
    }
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      unsigned char byte[4];
      unsigned long word;
    
      byte[0]=170;
      byte[1]=200;
      byte[2]=50;
      byte[3]=155;
    
      printf("%02X %02X %02X %02X\n", byte[0], byte[1], byte[2], byte[3]);
    
      word = byte[3];
      word |= byte[2] << 8;
      word |= byte[1] << 16;
      word |= byte[0] << 24;
    
      printf("%08X\n", word);
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./hw10
    AA C8 32 9B
    AAC8329B
    Last edited by itsme86; 11-10-2005 at 05:28 PM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Your solution needs knowledge of bitwise operators and bitshifting. See the FAQ article on this.

    After you've read that, you simply merge the four bytes together. Since byte[0] seems to be your most significant byte, shift it the farthest left:
    Code:
    my_integer = (byte[0] << 24) | (byte[1] << 16) | (byte[2] << 8) | byte[3];
    printf("%u", my_integer);
    Be sure my_integer is defined as unsigned as well.

    A word is 2 bytes, btw. The printf() you posted has an odd trailing "2" and the indecies are wrong. Use byte[0], byte[1], byte[2], byte[3].
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    4
    What can i say but a GREAT BIG THANK YOU
    Problem solved Lots of resect to you both

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    4
    <hangs head in shame>
    Problem solved but this has opened a new problem...... 'life?? don't talk to me about life'



    I can now printf my hex value in decimal but how can i get the decimal value to always be 10 digits by adding a leading 0

    if my result hex value is 09D87C67

    printf("%d",result")

    I would get 165182567
    I REALLY need 10 digit displayed
    0165182567

    Thanks in advance for your time

  6. #6
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by stormystones
    I can now printf my hex value in decimal but how can i get the decimal value to always be 10 digits by adding a leading 0
    Try using a format string like "%010d" -- the leading zero in the format length specifier tells it to zero pad.
    Insert obnoxious but pithy remark here

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    4
    Easy when you know how


    Yet again i am amazed by such a quick/correct reply....problem 2 solved.... thankyou!

    For now all my problems are solved (I did say for now)

    <<< also can i add i am learning from this and REALLY thank you all>>>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. Replies: 18
    Last Post: 03-26-2008, 09:01 AM
  4. Hex to four unsigned bytes
    By NoobieGecko in forum C Programming
    Replies: 9
    Last Post: 03-09-2008, 01:26 PM
  5. Passing structures to a function
    By earth_angel in forum C++ Programming
    Replies: 5
    Last Post: 07-13-2005, 06:13 AM

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