[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: printf hex conversion gives wrong values

From: Bob Proulx
Subject: Re: printf hex conversion gives wrong values
Date: Wed, 26 Jun 2002 14:07:06 -0600
User-agent: Mutt/1.4i

> during developing a new shell script; I detected an erroneous behaviour
> of the printf command.

Thank you for your report.  But I believe you are unaware that leading
zeros indicate octal encoding.  Neither 8 nor 9 are valid octal
values.  And of course 010 octal is 9 decimal and so on.

> printf fails on hex conversion for 008 and 009 and gives wrong values
> for higher numbers, if the digit to convert contains leading zeroes.
> Without leading zeroes the conversion succeeds.
> 2.04.21(1)-release

printf is both a shell built in and a standalone program.  You are
using the shell built in, thanks for including the shell version btw,
which if actually had a bug would need to go to address@hidden  :-)
It is confusing, I know, but this list is for the standalone programs.
In this case it does not matter at all so don't worry about it.  It is
just for future reference.

> # printf "%02x\n" 008
> bash: printf: 008: invalid number
> # printf "%02x\n" 009
> bash: printf: 009: invalid number
> # printf "%02x\n" 010
> 08
> # printf "%02x\n" 011
> 09

And if you had used the external program you would have seen this.

  /usr/bin/printf "%02x\n" 09
  /usr/bin/printf: 09: value not completely converted

Numbers with leading zeros indicate octal encoding.  Numbers with
leading 0x indicate hexadecimal encoding.  You can't put leading zeros
on there if you want them to be decimal.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]