[LinuxBIOS] Convert hex to dec

Peter Stuge peter at stuge.se
Wed Jul 4 17:29:56 CEST 2007


On Wed, Jul 04, 2007 at 09:22:54AM -0400, Joseph Smith wrote:
> Hello,
> Got another silly C newbie question.
> If I want to convert a hex value to decimal, would this work?
> 
> value = ff /* Hex value */
> 
> sscanf(value, %d, &value)
> 
> Is the variable "value" now 255??

Hexadecimal, decimal and octal are different ways for us humans to
express numbers to computers, but no matter what we use, they are
always stored in binary form in the machine.

Thus, there is no difference between a number in hexadecimal or
decimal. We do however have to tell the computer which formatting we
want when the computer should show us the numbers.

unsigned char value;

value=0xff; /* hexadecimal */

/* %d means print number in decimal */
printf("value in decimal is now %d\n",value);


value=135; /* decimal */

/* %x means print in hex */
printf("value in hexadecimal is now %x\n",value);


value=0254; /* octal */

if(0xac==0254)
  printf("C knows that 0xac == 0254 because they are both == %d\n",value);


Have a look at the printf man page for your nearest C library to
learn about all the good stuff you can put into formatting strings
besides just %d and %x.


//Peter




More information about the coreboot mailing list