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. 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```

3. 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].

4. What can i say but a GREAT BIG THANK YOU
Problem solved Lots of resect to you both

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

6. 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.

7. 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>>>