[R] R-help: conversion of long decimal numbers into hexadeci

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Thu Nov 10 18:28:11 CET 2005


On 10-Nov-05 Antje Döring wrote:
> Hi there,
> 
> could somebody help me to convert a decimal number into a hexadecimal
> number? I know that there is the function "sprintf", but the numbers I
> want to convert consist of  20 or more numbers. "Spintf" is not able to
> convert these big numbers.

If I understand aright, you have decimal integers with 20 or more
digits (and you want to get these as hexadecimal).

You are probably out of luck for a direct approach, since
10^20 > 2^64 (indeed > 2^66), so you will have overflowed a 64-bit
integer. However, I'm not sure what the limitations on integer
types are in R on all platforms.

If, however, all you need is to do these conversions, and you
do not really need to use R (how off-topic can I get ... ?),
then (at any rate on Linux/Unix systems where the program is
installed by default) you can use the aribitrary-precision
calculator 'bc'.

Session:

$ bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
obase=16

1234567898765432123456789
1056E0F555A18EBDA7D15

123456789876543212345678987654321
6163E6712EBBAA4E3D62B41F4B1

12345678987654321234567898765432123456789876543212345678987654321
1E02BC221DC9369C8981C6F859501BD313D339F09180862B41F4B1

quit


Und so weiter ... and of course you can go in the opposite
direction by "ibase=16" (to set hex as the input base) and
"obase=10" (to set decimal as the output base).

'bc' is a classic Unix tool, and features as an illoustration
of complex programming in C, with lex and yacc and all, in
"The Unix Programming Environment" (as I recall) by Kernighan
and Ritchie.

I don't need it often, but when you need it it's very handy
(e.g. now).

Hoping this helps,
Ted.

PS:

$ bc -l
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
scale=1000
pi=4*a(1)
pi
3.141592653589793238462643383279502884197169399375105820974944592307\
81640628620899862803482534211706798214808651328230664709384460955058\
22317253594081284811174502841027019385211055596446229489549303819644\
28810975665933446128475648233786783165271201909145648566923460348610\
......................
08302642522308253344685035261931188171010003137838752886587533208381\
42061717766914730359825349042875546873115956286388235378759375195778\
18577805321712268066130019278766111959092164201988

(last digit wrong because of truncation)


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 10-Nov-05                                       Time: 17:28:05
------------------------------ XFMail ------------------------------




More information about the R-help mailing list