How to merge two Hex numbers to one number and then convert it into decimal.?
22,662
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.
Author by
user46573544
Updated on December 04, 2020Comments
-
user46573544 over 3 years
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 asnum3=2571
and then I have to convert2571
into a decimal number. How do I do this? Please help, Thanks! -
Mariano Macchi about 9 yearsThis works :) but there's no need for &: num3= ((num1<<8)|(num2));
-
Nerdy about 9 yearsya.. I have made the change thanks @MarianoMacchi
-
Nerdy about 9 yearsYou 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 num2 .Logical or is the equivalent for combining, hence the
|
symbol. -
Hridaynath over 6 yearsExcellent! Answer