[LinuxBIOS] Convert hex to dec
uwe at hermann-uwe.de
Wed Jul 4 19:47:11 CEST 2007
On Wed, Jul 04, 2007 at 05:29:56PM +0200, Peter Stuge wrote:
> 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 */
> 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.
I use some aliases in my .bashrc which are pretty handy for converting
HEX or BINARY or DECIMAL:
alias HEX="ruby -e 'printf(\"0x%X\n\", ARGV)'"
alias DEC="ruby -e 'printf(\"%d\n\", ARGV)'"
alias BIN="ruby -e 'printf(\"%bb\n\", ARGV)'"
(You can replace 'ruby' with 'perl' if you want)
$ HEX 78
$ BIN 0x6a
$ DEC 0x72
http://www.hermann-uwe.de | http://www.holsham-traders.de
http://www.crazy-hacks.org | http://www.unmaintained-free-software.org
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 189 bytes
Desc: Digital signature
More information about the coreboot