473,509 Members | 12,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to convert very large number to hex string

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
Nov 14 '05 #1
4 5819
od******@gmail.com (oddstray) writes:
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?
Nov 14 '05 #2

"oddstray" <od******@gmail.com> wrote

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.
Nov 14 '05 #3
"Rufus V. Smith" <no****@nospam.com> wrote in message
news:1095187449.ytXxTpoXkQUXiWsoYr/jQA@teranews...

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.

--
Mabden
Nov 14 '05 #4
od******@gmail.com (oddstray) writes:
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.

Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1774
by: cpptutor2000 | last post by:
Could some C++ guru please help me with this problem? Suppose I have a string representation of a very large number as: char *strNum = "1234"; now suppose I want to store this number with each...
9
90038
by: FalkoG | last post by:
Hello colleague I want to convert a floating number for example 5236.9856982 to a hexadecimal number. I'd tried several things but my problem is to convert the position after decimal point. I...
5
36796
by: Steven | last post by:
I have tried to use FormatNumber() to convert a very large double type number to String format, however, the result seems not correct, is there any limitation for using FormatNumber() function? ...
30
17148
by: ceeques | last post by:
Hi I am a novice in C. Could you guys help me solve this problem - I need to convert integer(and /short) to string without using sprintf (I dont have standard libray stdio.h). for...
5
3892
by: bbb | last post by:
Hi, I need to convert XML files from Japanese encoding to UTF-8. I was using the following code: using ( FileStream fs = File.OpenRead(fromFile) ) { int fileSize = (int)fs.Length; int buffer...
52
1565
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
8
6236
by: =?Utf-8?B?V2hpdG5leSBLZXc=?= | last post by:
Hi there, I'm having a bit of trouble using an HRASCONN object as a class member variable inside my managed C++ class. When I call RasDial() and pass in the address of my HRASCONN object, I get...
1
5366
by: Sergei Shelukhin | last post by:
Hi. I have a string, 16 bytes long, representing a hex number. I need to convert it to a number and put it into pgsql database; it complicates the matters further cause pgsql doesn't support...
8
3648
by: te509 | last post by:
Hi guys, does anybody know how to convert a long sequence of bits to a bit-string? I want to avoid this: '949456129574336313917039111707606368434510426593532217946399871489' I would...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3219
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.