Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Decimal to hexadecimal conversion functions
Message
 
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00464326
Message ID:
00465356
Vues:
26
If you prefere "pure" functions....

All the best:
Nikolay Petkov

***
* Converts POSITIVE decimal integers to hex (Char)
* Writen by Jonathan Cohen, Modifyed by Nikolay Petkov
* Input: NUMERIC
* Output: CHAR
*
FUNCTION DecToHex
LPARAMETER tnInNum
IF m.tnInNum<=15
RETURN PADL(FindHex(m.tnInNum),2,"0")
ENDIF
LOCAL lcOutStr
lcOutStr = SPACE(0)
DO WHILE m.tnInNum>0
lcOutStr = FindHex(MOD(m.tnInNum,16)) + m.lcOutStr
tnInNum = INT(m.tnInNum/16)
ENDDO
RETURN m.lcOutStr
*
* Lookup table for conversion of alpha hex chars
FUNCTION FindHex
LPARAMETERS InVal && Integer 0-16
LOCAL jOutStr
DO CASE
CASE m.InVal=10
jOutStr='A'
CASE m.InVal=11
jOutStr='B'
CASE m.InVal=12
jOutStr='C'
CASE m.InVal=13
jOutStr='D'
CASE m.InVal=14
jOutStr='E'
CASE m.InVal=15
jOutStr='F'
OTHERWISE
jOutStr=STR(m.InVal,1,0)
ENDCASE
RETURN m.jOutStr

***
* This function converts POSITIVE hex (Char) to decimal
* Writen by Jonathan Cohen, Modifyed by Nikolay Petkov
* Input: CHAR
* Output: NUMERIC
*
FUNCTION HexToDec
LPARAMETERS tcInStr
LOCAL jLen, nSum, rPtr, nCtr
tcInStr=ALLTRIM(m.tcInStr)
jLen=LEN(m.tcInStr)
STORE 0 TO nSum, rPtr
FOR nCtr = 1 TO m.jLen
cPtr = UPPER(SUBSTR(m.tcInStr,m.jLen-m.rPtr,1))
DO CASE
CASE cPtr='A'
cPtr='10'
CASE cPtr='B'
cPtr='11'
CASE cPtr='C'
cPtr='12'
CASE cPtr='D'
cPtr='13'
CASE cPtr='E'
cPtr='14'
CASE cPtr='F'
cPtr='15'
ENDCASE
nSum = m.nSum + (VAL(m.cPtr) * 16^(m.nCtr-1))
rPtr = m.rPtr + 1
ENDFOR
RETURN INT(m.nSum)
Nikolay Petkov, nik@osf.bg
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform