Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Ho do I convert decimal to hex with a twist?
Message
From
05/08/1998 10:30:34
Dragan Nedeljkovich
Now officially retired
Zrenjanin, Serbia
 
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00123780
Message ID:
00124066
Views:
28
>>Just ? transform( INT( x ), "@0" )
>
>Hi David,
>
>That still gives me a result of 0x0000000A. We're looking for a way to convert a numeric value with decimal places to a value we can store in a non-numeric field. Thing is though the decimal places mean something in our scenario. And I take it hex is the wrong way to go since it only uses integers. Any other thoughts?

mantissa=right(str(x, 20, < number of decimals here >), < number of decimals here too >)
? transform(x, "@0")+"."+transform( val(mantissa), "@0")

You'll have to decide how many decimals you want to support. This way, though, you don't get real binary representation of your decimals as decimals, but as an integer of the chosen length... in theory, you should build a string of bits:

mantissa=x-int(x)
char=0
i=0
hexstr=""
do while mantissa>0
mantissa=2*mantissa && left shift by one bit
char=char*2 && left shift the result
if mantissa>1
char=char+1 && add the overflow, if any
mantissa=mantissa-1 && discard the overflow
endif
i=i+1 && bit counter
if i=4 && four bits make one hex digit
hexstr=hexstr+chrtran(char+48,":;<=>?") && in case HTML eats this, chr(59) to chr(63)
char=0 && reset for the next hex char
endif
enddo

Something like this. I haven't done this calculation for twenty years or so, but that's the principle: you keep doubling the mantissa, and write a binary 1 everytime it overflows, else you write a zero; discard the overflow and continue for as long as you like or untill the decimals are consumed.

Of course, Geo was quicker with my favorite BinToC() pet :)

back to same old

the first online autobiography, unfinished by design
What, me reckless? I'm full of recks!
Balkans, eh? Count them.
Previous
Reply
Map
View

Click here to load this message in the networking platform