Operating System - HP-UX
1822146 Members
4214 Online
109640 Solutions
New Discussion юеВ

Large Decimal to Hex Conversion in C

 
Brad Simmonds
New Member

Large Decimal to Hex Conversion in C

I have a problem in that I need to change a large decimal value into its hex equivalent. The largest decimal value I need to represent a hex is 25516777215. The problem here is that this number is larger than any of the C functions can handle. We are usng UP-UX 11. I was olso told that a big number library might be the answer, does HP have that available?

Any suggestions on how to go about this would be appreciated.

Thanks,
Brad.
8 REPLIES 8
Martin P.J. Zinser
Honored Contributor

Re: Large Decimal to Hex Conversion in C

Hi Brad,

since you are looking for a HP-UX library and not for an hp OpenVMS one, you most probably will get much better answers in the hpux/language area at

http://forums.itrc.hp.com/cm/CategoryHome/1,,150,00.html

Greetings, Martin
Antoniov.
Honored Contributor

Re: Large Decimal to Hex Conversion in C

Hi Brad,
I answer as OpenVms works, but HP-Ux may be different.
On OpenVms you can compile using long type 32 or 64 bit. Naturally with 32 bit long you cannot convert 25516777215 but using 64 bit long you can convert using standard sscanf function.
If you cannot found 64 bit long in HP ux write again: send you a function to convert a string to a hex string (not is possible manager numeric in this case).

HTH
Antoniov
Antonio Maria Vigliotti
A. Clay Stephenson
Acclaimed Contributor

Re: Large Decimal to Hex Conversion in C

You don't need to do anything special except compile with the +DD64 option. This will coerce all longs to 64-bits but this will only run on PA-8XXX processors and above.
If it ain't broke, I can fix that.
Brad Simmonds
New Member

Re: Large Decimal to Hex Conversion in C

First, sorry for posting in the wrong group.

Clay you were right. I added the +DD64 flag to the compilation line and it worked. I asked one of our UINIX team, they did a little research and came up with the same suggestion so I tried it and it worked.

Antoniov mentioned the sscanf to convert it which I was unfamiliar with, scanf did not handle the large number and I was unfamiliar with sscanf at the time. I ended up using atof and cast the result as an unsigned long long which truncated the decimal places from the result. I tested the $%#@ out of it to ensure it was going to work correctly and it did.

Thanks for taking the time to post suggestions. I am very appreciative.

Brad.
H.Merijn Brand (procura
Honored Contributor

Re: Large Decimal to Hex Conversion in C

And if you have *realy* large numbers, you can use Math::bigint :)

pc03:/W 283 $ perl -Mbigint -le'$x = 24354**245; print $x->as_hex'
0x44feca8dca03b4a7f6c74b327fb1f5fce5853557cfdb7ee961d45dca4f33a899df3ef7f213b050
4e58c65946eb54bc307f4d62b6d3ae93495f55df69b154b1666ebea5233cb9b2a455d2f1e2758c75
2fe932781920d56cc8060f3020bb00736ea407783194ab3837500f021cb0bb3b022b4cf69dd4d71c
ab55194f3865562753cb1718fd947e25806a914cb29011d4dc476983d0a3057f0ae0798805379fb2
157e07eb353211f14e821df9f5e198dc4fcc356c42dc59cd692931c02398644031c9740423839923
dabe0d8da87849945915907bd6eebff1f03c5b63726cd9aaf9d602f204cfd29ae23dfec82bc99dc3
a05d6e0fb8be22ace5459fc1caab2a9c1410d507fd7b94e7496608c2688b5b691542da03ea8ea391
8adbc92e17a791bcab856eb83522148cdd03f41b485cb10f4762d5c8a307a24e2a521d2936cb23ff
72c5a21872d099f374ecc05b73a0557a166f28ea1dfd1279694a0c159dd527e73c3552bcc01d009c
dd3ccf7eb2e2963fc732cc351ac2846134faa28f57f8d42e021516e9d19ce463576be4c628a59b80
a1e730c73da905ed934c253791060d8da20000000000000000000000000000000000000000000000
000000000000000
pc03:/W 284 $

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Ron_100
New Member

Re: Large Decimal to Hex Conversion in C

You can use a tool on my site, http://www.ronshardwebapps.com/numbers.asp, or if you want to convert very large numbers, just script out the manual remainder division. You will find it to be very simple and can be done in a few lines of code in a Do Until...Loop type construct.
Erik Hawkins
New Member

Re: Large Decimal to Hex Conversion in C

That's a nice little tool ron...thank's for posting.
Ron_100
New Member

Re: Large Decimal to Hex Conversion in C

thanks, i think i messed up the link, it has a comma at the end of it, it should be http://www.ronshardwebapps.com/numbers.asp