Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Converting to hex
Message
From
27/02/2002 09:58:21
 
 
To
27/02/2002 05:42:30
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00625576
Message ID:
00625722
Views:
12
Hi Andy.

>>If I have a decimal number, how do I convert it to hex in my program?
>
>TransForm( , "@0" ) to get a value into hex, and EVAL() to get it back

That works if you want a string such as "0x00000100". However, if you're working with a binary file (such as a VFP table) and want to write a decimal value (eg. a record count), you'll need something like this instead:
lparameters tnValue, ;
    tnPlaces
local lnPlaces, ;
    lcHex, ;
    lnDecimal, ;
    lnCurrDecimals, ;
    lnPlaces, ;
    lnI, ;
    lnExponent, ;
    lnTemp, ;
    lcOut
lnPlaces       = iif(pcount() = 1, 4, tnPlaces)
lcHex          = ''
lnDecimal      = tnValue
lnCurrDecimals = set('DECIMALS')
lnPlaces       = iif(pcount() = 1, 4, tnPlaces)
set decimals to 17
for lnI = lnPlaces to 1 step -1
    lnExponent = 256 ^ (lnI - 1)
    lnTemp     = int(lnDecimal/lnExponent)
    lcHex      = lcHex + chr(lnTemp)
    lnDecimal  = lnDecimal - lnTemp * lnExponent
next lnI
set decimals to lnCurrDecimals

* Reverse the order of the characters.

lcOut    = ''
for lnI = 1 to lnPlaces
    lcOut = lcOut + substr(lcHex, lnPlaces - lnI + 1, 1)
next lnI
return lcOut
Doug
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform