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.

Share:
22,662
user46573544
Author by

user46573544

Updated on December 04, 2020

Comments

  • user46573544
    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 as num3=2571 and then I have to convert 2571 into a decimal number. How do I do this? Please help, Thanks!

  • Mariano Macchi
    Mariano Macchi about 9 years
    This works :) but there's no need for &: num3= ((num1<<8)|(num2));
  • Nerdy
    Nerdy about 9 years
    ya.. I have made the change thanks @MarianoMacchi
  • Nerdy
    Nerdy about 9 years
    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 num2 .Logical or is the equivalent for combining, hence the | symbol.
  • Hridaynath
    Hridaynath over 6 years
    Excellent! Answer