Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two Hex numbers to one number and then convert it into decimal.?

I am making a C program in which I have two hex numbers, i.e. num1=25 num2=71 which are in hex. I want to make it as num3=2571 and then I have to convert 2571 into a decimal number. How do I do this? Please help, Thanks!

like image 222
user46573544 Avatar asked Mar 25 '15 09:03

user46573544


1 Answers

Just shift the digits and combine

int num1,num2,num3;
num1=0x25;
num2=0x71;
num3=(num1<<8)|(num2);
printf("%x %d",num3,num3);

You need to place 25 (0025) followed by 71 (0071) in a variable, so you have to left shift the first number by 8 bits (0025 to 2500) and combine it with num2. Logical Or is the equivalent for combining, hence the | symbol.

like image 199
Sathiya Avatar answered Sep 28 '22 06:09

Sathiya