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 */
>
> 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.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[0])'" alias DEC="ruby -e 'printf(\"%d\n\", ARGV[0])'" alias BIN="ruby -e 'printf(\"%bb\n\", ARGV[0])'" (You can replace 'ruby' with 'perl' if you want) Usage: $ HEX 78 0x4E $ BIN 0x6a 1101010b $ DEC 0x72 114 HTH, Uwe. -- http://www.hermann-uwe.de | http://www.holsham-traders.de http://www.crazy-hacks.org | http://www.unmaintained-free-software.org
signature.asc
Description: Digital signature
-- linuxbios mailing list [email protected] http://www.linuxbios.org/mailman/listinfo/linuxbios
