Re: converting hex strings to decimal digits strings

New Message Reply About this list Date view Thread view Subject view Author view

Martin G. Diehl (mdiehl@nac.net)
Tue, 24 Mar 1998 20:07:53 -0500


Eric Young wrote:
>
> On Tue, 24 Mar 1998, Martin G. Diehl wrote:
> > > how to convert a byte string (a big integer) like 0x59, 0x42, 0x10
> > > (MSB) to a digits string like 5849616 suitable for ascii display
> > > 0x59*256^2 + 0x42*256 +0x10 = 5849616
> > > the bignum libs i'm using doesn't have that :(
>
> > digit := NumberToConvert mod 10;
> > result := result + digit;
> > NumberToConvert := NumberToConvert div 10;
>
> One simple improvment it not to use '10' but use the largest size
> power of 10 that will fit in the word size of the machine. For a
> 32 bit machine, 1000000000L can be used. To save the mod, just keep
> on divinding the number by 1000000000L, saving the result, then when
> you have 0 left, print out the numbers (in reversed order :-). You
> will probably have to do a little playing around to drop the leading
> 0's on the first number printed.
>
> eric

In that case you would be getting digits starting at the left as
though you were doing division "by hand" -- forgetting for the moment
about negative numbers and assuming that the number is > zero, I think
your algorithm would be similar to:

        result := '';
        DivideBy := 1000000000L;
        while DivideBy > 0 do
            begin
                digit := 0;
                while NumberToConvert > DivideBy do
                    begin
                        digit := digit + 1;
                        NumberToConvert := NumberToConvert div DivideBy;
                    end;
                result := result + chr(ord('0') + digit);
                DivideBy := DivideBy div 10;
            end;

I think that's what you said.

Although you used the largest power of 10 that can be expressed in a
32 bit integer, you should use the largest power of 10 that can be
expressed and handled by your BigNum library. In both this and my
prior example, I intended that div and mod would be done by
the corresponding BigNum Library functions.

-- 
Martin G. Diehl

I am what I am. All opinions expressed within are strictly my own.

If Ziggy says "Time is what keeps everything from happening at once", and Newton teaches that Gravity brings all matter together, could we say that Time and Gravity have an antagonistic relationship?


New Message Reply About this list Date view Thread view Subject view Author view

 
All trademarks and copyrights are the property of their respective owners.

Other Directory Sites: SeekWonder | Directory Owners Forum

The following archive was created by hippie-mail 7.98617-22 on Fri Aug 21 1998 - 17:16:13 ADT