Question:
how do i convert hexadecimal value into decimal values in c ?
sree_msn
2006-04-15 10:12:07 UTC
Infact, this is 4 my 8051 program which i'm doing in c which shall be converted into assembly language using a cross compiler.
Three answers:
morgan
2006-04-15 17:19:22 UTC
If you want to convert a hex string to decimal, you

need to do two operations:



- convert the hex string to a number

- generate a decimal string from that number



If your system supported a full C implementation,

it would be as simple as using scanf()/printf().

I presume you're being asked to implement the

primitives yourself :-)



To convert a hex string to a number, you might

implement something like:



int number = 0;

while ((c = getchar()) != '\n') {

  if (c >= '0' && c <= '9')

    number = (number<<4) + (c-'0')

  else if (c>='a' && c<='f')

    number = (number<<4) + (c-'a')

  else if (c>='A' && c<='F')

    number = (number<<4) + (c-'A')

}



This could be cleaned up if you have access to the

macros [ie. isdigit() and tolower()].



To convert the number to a decimal string, you need

to perform successive divisions by 10. An 8051

doesn't have a hardware divide, but that's OK

because you should have a software divide/remainder

subroutine.



There are two tricks to keep in mind. First to

generate the appropriate character for each digit,

you can treat a literal character string as an

array. For example:



char c = "0123456789ABCDEF"[i];



will assign the correct character to variable "c"

based on the value of "i" (if it is between 0 and

15).



The other detail is that as you call the divide/

remainder subroutine, you will generate the digits

working from least significant to most significant.

Therefore, you'll want to fill an array from right

to left. Assuming you have a divide routine with

the prototype:



int

divide(int dividend, int divisor, int *remainder)



and you can use the "puts()" function to display the

generated string, you might use something like:



void

todecimal(int value)

{

  char buffer[20];

  char*p = &buffer[sizeof(buffer)-1];

  int remainder;

  while (value != 0) {

    value = divide(value, 10, &remainder);

    *p-- = "0123456789ABCDEF"[remainder];

  }

  puts(p+1);

}
run4ever79
2006-04-15 12:22:00 UTC
As mentioned you can do the division, but in C you can use the bit-shift operator to do fast division (by powers of 2). For example foo >> 4 will divide by 16, but the operation is faster than the division operator on most systems.



The divison will go something like this, since you need the remainder too.



int quotient = num;

int remainder;

while(quotient >> 4){

remainder = quotient << sizeof(int) * 2 - 4; /* Use a macro to compute sizeof(int) *2 - 4 at compile time */



quotient = quotient >> 4;



/* store this remainder somewhere and visit it in reverse order that it was computed. */

}
Sunny
2006-04-15 10:33:38 UTC
A HEX->DECI converter function also does the same what we know of it. Since, you know how to write programmes in C, follow the usual steps and create your own logic for it.



It is always good to create the ideal code(HEX->DECI) best suitable for you own code.



Simply go with the continious division of 16 [The most preffered than other methods like the tabular one] and write the remainders exactly as while BINARY->DECI conversion using 2.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...