Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Binary to Hex Conversion
Message
De
16/06/1997 19:29:23
 
 
À
16/06/1997 17:32:57
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00036266
Message ID:
00036592
Vues:
32
What about negative numbers? :)

Vlad

>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
Fil
Voir

Click here to load this message in the networking platform