Re: Trying to pad a 40 digit number with zeros with no luck



On Jun 11, 4:15 am, Wayne <nos...@xxxxxxxxxxxxxx> wrote:
Tonagon wrote:
Hey gang!
I am working on a script in SCO using the Korn shell.
I found printf and typeset -Z but have had no luck getting them to
work for me, possibly due to the size of the number in question.

I am pulling a 10 character Hex value from a database, converting Hex
to binary with dc (this is then a 40 character number) so I can then
update the 13th digit to be a 1.
Then I have to convert it back to Hex again and import it back into
the database.
All this to turn on an option bit!

Using the dc command to convert from Hex to Binary works, except in
the cases where the beginning of the Hex is zeros.  The Hex string has
stay 10 characters long, but when I convert those with a zero or two
at the beginning those zeros get dropped.  I need those zeros to stay
there so that when I make the 13th digit a 1 it is actually the 13th
digit.

Anyway, does anyone know how I can pad a 40 character binary number
with zeros at the beginning?

OR should I not use dc and find a better way to convert from Hex to
Binary that won't drop the leading zeros?

Here is the output I get on the printf and typeset commands:

# printf '%.40d\n' 00001000000000100000000010000000111
printf: 00001000000000100000000010000000111: Result too large or too
small (error 34)
0000000000000000000000000000002147483647

The same command works fine with a smaller number.

typset almost works, but I get more data back then I want.
Like this:
# typeset -Z40 vareg3=1001111000001 echo $vareg3
ksh: 0000000000000000000000000001001111000001: is not an identifier

If the ksh: and : is not an identifier were not there, then that would
probably work fine.

What do you all think is the solution?
Many thanks in advance!

Tonagon.

P.S. here is the body of the script as it stands now (obviously still
in development):
#! /bin/sh
HOME=/usr/ajrob727/MAGCARDREQ/8700sql_magcardscripts
FILE1=$HOME/uws_bit.out
#
for i in `cat $FILE1|awk '{print $2}'|cut -b 2-11`; do
        echo $i
        var_bin=`echo 2o16i${i}p|dc`
        echo $var_bin
        # Command to pad binary output to 40 characters with zeros.
        var_pad=`printf '%.40d\n' $var_bin`
        echo $var_pad
        sleep 10
done

There is no need to convert the number or use bc or dc.
Ksh supports POSIX standard arithmetic and hex numbers.
This includes bitwise OR and shift.

Probably this will work in any POSIX shell but I haven't
really checked into it.  (When I'm wrong someone always is
quick to tell me. :-)  It does work in bash too.

-Wayne

====================Cut-Here=====================

#! /bin/sh
FILE1=$HOME/uws_bit.out
BIT=13  # bit to turn on

for i in $(awk '{print $2}' $FILE1 |cut -b 2-11)
do
    ORIG_NUM="0x${i}"  # make shell treat num as hex
    NEW_NUM=$(( ORIG_NUM | 1 << BIT ))
    printf 'original: %#x \t new: %#x\n' "${ORIG_NUM}" "${NEW_NUM}"
done

====================Cut-Here=====================

Here's the results of a short test (in ksh and bash)
I ran:

BIT=5
for i in 0 1 a 10 20 2A
do
    ORIG_NUM="0x${i}"  # make shell treat num as hex
    NEW_NUM=$(( ORIG_NUM | 1 << BIT ))
    printf 'original: %#x \t new: %#x\n' "${ORIG_NUM}" "${NEW_NUM}"
done
original: 0x0    new: 0x20
original: 0x1    new: 0x21
original: 0xa    new: 0x2a
original: 0x10   new: 0x30
original: 0x20   new: 0x20
original: 0x2a   new: 0x2a- Hide quoted text -

- Show quoted text -

Wow, I am overwhelmed with suggestions. I had never head of the
bitwise command or the shift command so that is cool stuff, thanks.
Shows what I know huh?
I gave this one a try, but I am getting an error from the "for i in
$..." line. It says:
Test.sh: syntax error at line 7: `$' unexpected.

I tried putting front tick back tick around the line, and removing the
$... but then I thought "Hey! Just ask Wayne about it".
What do you think?
.



Relevant Pages

  • Trying to pad a 40 digit number with zeros with no luck
    ... I found printf and typeset -Z but have had no luck getting them to ... I am pulling a 10 character Hex value from a database, ... the cases where the beginning of the Hex is zeros. ...
    (comp.unix.shell)
  • Re: Trying to pad a 40 digit number with zeros with no luck
    ... I found printf and typeset -Z but have had no luck getting them to ... I am pulling a 10 character Hex value from a database, ... the cases where the beginning of the Hex is zeros. ...
    (comp.unix.shell)
  • Re: Converting Floats to Strings yields erratic results
    ... compare the results to printf(), ... I get an impossibly clean result of 1 followed by loads of zeros, ... after the last significant digit of the output string. ... usually seen real implementations do. ...
    (comp.lang.c)
  • Re: RosAsm
    ... wolfgang kern wrote: ... > I'll extend with zeros to get 32 bytes right now. ... don't see any reason why Rene would ever want to do that, ... C1 80" _should_ work to put hex straight into the code, ...
    (alt.lang.asm)
  • Re: printf
    ... > I know the error is in printf. ... Either too many zeros or the decimal must be wrong. ... > object files for financial analysis without GUI graphics. ...
    (comp.lang.c)