Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Converting to hex
Message
De
27/02/2002 09:58:21
 
 
À
27/02/2002 05:42:30
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00625576
Message ID:
00625722
Vues:
16
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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform