How to convert decimal number into a hexadecimal number then store the corresponding result in an integer as a decimal value

c
11,633

Solution 1

Is your question, converting the decimal number into hex format and then save that hexa number in an integer. Then this will do the trick

#include<stdio.h>
#include<stdlib.h>
int main()
 {
   char x[6];    'just took 6,you can increase its size as per your int number
                 ' Though it uses only 3 elements in these example
   int i;
   int a;
   i=837;              ' Load the i value with a decimal number say 837
   sprintf(x,"%x",i);  ' pass the hex format value to string x instead of stdout
                       ' x contains x[0] ='3' x[1] ='4' x[2] ='5'
   a=atoi(x);          ' Convert the string format to integer
   printf("%d",a);     ' outputs 345
 }

Note The Hexanumbers may even consist ABC and for that , I dont know exactly what you are logic will be ? ignore it or any other transformation? what would you store if the hexa number is 3ab ?

Solution 2

So, after reading the question carefully, implementing pmg's answer, consider:

#include <stdio.h>

int main() {
  int i = 0x345;
  int j = 1;
  int result = 0;
  do { 
    result += (i&0xf)*j;
    j *= 10;
  } while (i/=(0x10));
  printf("%i\n", result);
}

this works well, unless there are hex-digits (i&0xf) > 0x9.

For example, in case of int i = 0xABC the range of the decimal digit is obviously not sufficient to represent (as stacker has pointed out) the number s.t. this hex-value would produce something like 1122. I don't know what you would expect the function to produce in this case. Maybe you should extend your specification.

Solution 3

I'm not sure what you want to do, a hex digit range is from 0..F you can't store an arbitrary hex value in a decimal digit (0..9). The hexvalue 345 is 837 in decimal. Another valid hex representation say "ABC" couldn't be converted in the way you described.

To convert the value from hex to integer you could use sscanf

with format string "%x"

sscanf ("345","%x" ,&i);

After that i would be 837.

Note that the preceding "0x" has been omitted.

Share:
11,633
Anudeep
Author by

Anudeep

Updated on November 21, 2022

Comments

  • Anudeep
    Anudeep over 1 year

    In a C program, I stored a decimal number 837 in an integer, its Hexdecimal value is 0x345. Now I want to store the same Hexdecimal value of that variable to integer variable as 345. How to do it?

    • Peter G.
      Peter G. over 12 years
      What about hexadecimal digits > 9?
  • Anudeep
    Anudeep over 12 years
    Thanks for your reply, but how can i multiply with that hex number? i am storing as 837 in a variable, its Hex representation is 0x345.
  • Anudeep
    Anudeep over 12 years
    Thanks for your reply but am asking the senario for the stored variable for which i want to isolate each digit of its Hex form
  • Anudeep
    Anudeep over 12 years
    Thanks for your reply but am asking the senario for the stored variable for which i want to isolate each digit of its Hex form, as you said am storing as 837 but i want to isolate each digit of its hex value
  • nos
    nos over 12 years
    @Anudeep do you want each digit in a different variable ? And what kind of variable have you stored your hexadecimal number in ? please update your question - such information is important.
  • pmg
    pmg over 12 years
    To isolate the least-significant 4 bits you can do variable & 15; the next 4 bits with (variable & 240) >> 4; ... note: 15 is 0xF and 240 is 0xF0
  • moooeeeep
    moooeeeep over 12 years
    mind the null-termination that is added by sprintf to the number (in this case writing x[4] = 0) to avoid a buffer overflow ...
  • Steve Jessop
    Steve Jessop over 12 years
    FWIW, (sizeof(int)*CHAR_BIT + 3)/4 + 1 would be a big enough array for any input. Also, although I doubt you'll find an implementation where it matters, the x format specifier takes an unsigned int argument, and you've given it an int.
  • cokedude
    cokedude about 10 years
    How do you deal with a, b, c, d, e, and f?