between the message and the code and the UT software considered
my code as a signature... I guess.
> I need to convert a binary number to Hex, and vice-versa. It's been a
long
> time since I've done this. Does anyone have some code that will do this
for
> me?
>
> The binary number will look something like: 1100001111010001 (It will be
as
> much as 16 digits)
I just wrote this program. It will covert a number from one base to another
whatever the base is. Check it out. You might want to test it, I didn't
had enough time but it seems to work so far.
If you want to convert a binary number like 10101101 to hex, call
the function like this :
?Base2Base('10101101',2,16)
For a hex2bin conversion :
?Base2Base('FF',16,2)
For all : If you think this might be usefull for somebody else
I will upload it on UT.
FUNCTION BASE2BASE
* Convert integers from one base to another
*
* lcNumber - the number to be converted as string
* lnBaseIn - the base of the number to be converted
* lnBaseOut - the base of the result
*
* return value is string
*
* The Letter2Number and Number2Letter functions
* have to be inserted in the procedure file or ... wherever
* can be found by the Base2Base
lParameters lcNumber,lnBaseIn,lnBaseOut
LOCAL lnCount, lnDecimal, lnNumberLen, lnRest, lcResult, lnDRes
If Type('lnBaseIn')='L'
lnBaseIn = 10
EndIf
If Type('lnBaseOut')='L'
lnBaseOut = 10
EndIf
if lnBaseIn <> 10
lcNumber = Allt(Upper(lcNumber))
lnNumberLen = Len(lcNumber)
* conversion 2 decimal
lnDecimal = 0
FOR lnCount = 1 TO lnNumberLen
lnDecimal = lnDecimal +
Letter2Number(substr(lcNumber,lnCount,1))*lnBaseIn^(lnNumberLen-lnCount)
ENDFOR &&* lnCount = 1 TO lnBaseIn
else
lnDecimal = val(lcNumber)
endif
if lnBaseOut <> 10
lcResult = ''
do while .t.
lnRest = MOD(lnDecimal,lnBaseOut)
lnDRes = int(lnDecimal/lnBaseOut)
lcResult = Number2Letter(lnRest) + lcResult
if lnDRes<>0
lnDecimal = lnDRes
else
exit
endif
enddo
else
lcResult = str(lnDecimal)
endif
Return lcResult
Function Letter2Number
lParameters lcNumber
do case
case Between(ASC(lcNumber),48,57)
lnResult = ASC(lcNumber)-48
case Between(ASC(lcNumber),65,90)
lnResult = ASC(lcNumber)-55
otherwise
lnResult = 0
EndCase
Return lnResult
Function Number2Letter
lParameters lnNumber
do case
case Between(lnNumber,0,9)
lcResult = CHR(lnNumber+48)
case Between(lnNumber,10,45)
lcResult = CHR(lnNumber+55)
otherwise
lcResult = '?'
EndCase
Return lcResult
Précédent
Suivant
Répondre
Voir le fil de ce thread
Voir le fil de ce thread à partir de ce message seulement
Voir tous les messages de ce thread
Voir tous les messages de ce thread à partir de ce message seulement