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

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)

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!


P.S. here is the body of the script as it stands now (obviously still
in development):
#! /bin/sh
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

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.



#! /bin/sh
BIT=13  # bit to turn on

for i in $(awk '{print $2}' $FILE1 |cut -b 2-11)
    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}"


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

for i in 0 1 a 10 20 2A
    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}"
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: 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?