how to convert very large number to hex string

O

oddstray

Hi,

I have a number which is larger than the max unsigned long int. I
don't have 64-bit integers available to me. I need to get the
resulting 40-bit hex string.

I can't find any algorithm or solution when I search the web. I've
seen a couple of suggestions searching Usenet, but neither of them
works. One tried to use a union, and the other tried to convert each
byte of the float.

There are a few open-source large-value math libraries, but none of
them seems to include float-to-hex-string conversion. (And I find
open-source nearly impossible to build and understand and use anyway.)

I have an algorithm which works, but seems alarmingly baroque ... so
I'm hoping there's a more straightforward way. It divides the large
value by six, converts the sixths to hex strings, and then six times
does a character-by-character addition to construct the final hex
string.

It seems that some sort of bit magic should be possible, but if I ever
knew enough about how floats are rendered I've long since forgotten
it. Possibly it's machine-dependent, anyway.

Can anyone offer any enlightenment? Is my baroqe algorithm really the
best one? Thanks in advance ...


B
 
P

Pete Glasscock

I have a number which is larger than the max unsigned long int. I
don't have 64-bit integers available to me. I need to get the
resulting 40-bit hex string.

How is your number stored?
 
M

Malcolm

oddstray said:
I have a number which is larger than the max unsigned long int. I
don't have 64-bit integers available to me. I need to get the
resulting 40-bit hex string.

It seems that some sort of bit magic should be possible, but if I ever
knew enough about how floats are rendered I've long since forgotten
it. Possibly it's machine-dependent, anyway.
You need to write your own arbitrary-precision integer arithmetic routines.
You can use the algorithms you learned in primary school, but in binary
rather than in decimal.

Conversion to hex is easy. Each hex digit represents 4-bit nybles of your
binary number. Simply look up.
Conversion to decimal is more difficult. As part of your division routine
you will calculate the modulus. You need to calculate modulus ten and then
divide by ten repeatedly.
Floating-point numbers are stored in an internal format. There is no easy
way of converting to and from large integers - you have to take a chunk at a
time and do the calculation.
Make sure that double is not accurate enough for your purposes before going
to all this trouble.
 
R

Rufus V. Smith

oddstray said:
Hi,

I have a number which is larger than the max unsigned long int. I
don't have 64-bit integers available to me. I need to get the
resulting 40-bit hex string.

I can't find any algorithm or solution when I search the web. I've
seen a couple of suggestions searching Usenet, but neither of them
works. One tried to use a union, and the other tried to convert each
byte of the float.

There are a few open-source large-value math libraries, but none of
them seems to include float-to-hex-string conversion. (And I find
open-source nearly impossible to build and understand and use anyway.)

I have an algorithm which works, but seems alarmingly baroque ... so
I'm hoping there's a more straightforward way. It divides the large
value by six, converts the sixths to hex strings, and then six times
does a character-by-character addition to construct the final hex
string.

It seems that some sort of bit magic should be possible, but if I ever
knew enough about how floats are rendered I've long since forgotten
it. Possibly it's machine-dependent, anyway.

Can anyone offer any enlightenment? Is my baroqe algorithm really the
best one? Thanks in advance ...

I want to restate this question, if I may:

You currently have a floating point variable, which is holding a value
which,
if it were an integer, would be 40 bits long?

Hex conversion of integers is straightforward, I assume you can do it for
8/16/32 bit quantities.

I'd suggest dividing it down into 32-bit or 16-bit integral quantities and
doing normal hex conversion, then concatenating the strings.

Rufus

Please post the divide-by-six code, I can't imagine what you are talking
about there.
 
M

Mabden

Rufus V. Smith said:
Please post the divide-by-six code, I can't imagine what you are talking
about there.

Could he mean divide by 16? I think he is taking the dictionary meaning
of "hexa" meaning 6 as in hexagon, instead of "hexadecimal" meaning 6 +
10.

This could be homework.
 
T

Tim Rentsch

I have a number which is larger than the max unsigned long int. I
don't have 64-bit integers available to me. I need to get the
resulting 40-bit hex string.

I have an algorithm which works, but seems alarmingly baroque ... so
I'm hoping there's a more straightforward way. It divides the large
value by six, converts the sixths to hex strings, and then six times
does a character-by-character addition to construct the final hex
string.

Reading between the lines, it seems like what you're looking for is
something that takes a 'double' holding a non-negative integral value,
and returns the string hexadecimal representation for that.
Presumably there is enough precision in whatever is holding the value
so it is represented exactly.

If that's right, try this:

char *
double_to_hex_string( double value ){

static char result[1000];
char *p = result;
char *p_bound = p + sizeof(result) - 1;

double v = value;
double a = 1.0;

if( v < 0 ) return "(value is negative)";

while( a * 16 <= v ) a *= 16;

while( a >= 1 ){
int m;
for( m = 0; m < 16 && (m + 1) * a <= v; m++ ) {}
if( p < p_bound ) *p++ = "0123456789abcdef"[ m ], *p = 0;
v -= m * a;
a /= 16;
}

return result;
}

Doing something reasonable for negative or fractional inputs is
an "exercise for the reader". Also the buffer holding the
calculated string is just a static function variable, which
isn't really good for function re-entrancy, etc; I thought it
would suffice for purposes of illustration.

By the way, I don't make any claims that there aren't some
weird corner cases or strange architectures where this code
will mess up in some unexpected way. I do think it might
solve the problem you're trying to solve.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top