Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Decimal to hex conversion failing for large numbers

401 views
Skip to first unread message

Sharad

unread,
May 8, 2007, 12:00:45 AM5/8/07
to
Hi

I am converting a number to hex format as shown below, but conversion
failing for large numbers.

% set num 1487
1487
% set hex_num [format "%x" $num]
5cf
% set num 5357142840
5357142840
% set hex_num [format "%08X" $num]
integer value too large to represent
% set tcl_patchLevel
8.4.11
%

Please let me know if there is another command or if you have
written a proc to convert integer to hex.

TIA
Sharad

Sharad

unread,
May 8, 2007, 12:12:29 AM5/8/07
to

Stéphane A.

unread,
May 8, 2007, 2:20:53 AM5/8/07
to
Hi,

Computers handle integers as 32- or 64-bit values. A 32-bit integer
can hold 8 hexadecimal digits, whereas a 64-bit can hold 16 of them.
So the solution is to use arbitrary-length integers,

- either with math::bignum tcllib package
- either with the simple proc that you will find on the wiki.
--> Please see http://wiki.tcl.tk/16154.

Cheers,
Stéphane

suchenwi

unread,
May 8, 2007, 3:12:53 AM5/8/07
to
Or use Tcl 8.5 (the given number obviously fits into 64 bits):

(6247) 1 % format %x 5357142840
3f4f8338
(6247) 2 % info pa
8.5a4

Mark Janssen

unread,
May 8, 2007, 6:20:12 AM5/8/07
to
> Stéphane- Hide quoted text -
>
> - Show quoted text -

Note that using the native bigint support in Tcl 8.5 dramatically
improves the performance for this type of task.

A comparision of the baseconvert proc at the wiki page above and the
following proc, clearly demonstrates this.

proc hex {num} {
set res {}
set hex_list {0 1 2 3 4 5 6 7 8 9 A B C D E F}
while {$num / 16 != 0} {
set rest [expr {$num%16}]
set res [lindex $hex_list $rest]$res
set num [expr {$num/16}]
}
set res [lindex $hex_list $num]$res
}

set a
25543398472347234723447294729472384329374982742984729472347247729472984264726487264284628462846274628462846284628462846284623874623874623784623486248726487642846
% string length $a
161
% time {baseconvert 10 16 $a} 100
879291.28 microseconds per iteration
% time {hex $a} 100
4507.07 microseconds per iteration
% info patch
8.5a6

Mark

Eric Hassold

unread,
May 8, 2007, 8:31:46 AM5/8/07
to
Hi,

suchenwi wrote :


> Or use Tcl 8.5 (the given number obviously fits into 64 bits):
>
> (6247) 1 % format %x 5357142840
> 3f4f8338

8.5, but also 8.4 with built with "--enable-64bits", has support for 64
bits, but note from example above that %x format string (just like %d)
takes int32 as argument, and returns only hexa for 32 lower bits (8
digits), instead of 13f4f8338 as one may expect.

To format 64 bits integers, you need to explicitely use "l" modifier:

% format %lx 5357142840
13f4f8338
% format %ld 5357142840
5357142840

but:


% format %x 5357142840
3f4f8338

% format %d 5357142840
1062175544

Eric

-----
Eric Hassold
Evolane - http://www.evolane.com/

0 new messages