Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Binary to Hex Conversion
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00036266
Message ID:
00036299
Vues:
49
>>>>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)
>>>>
>>>>Craig
>>>
>>>Hi Craig,
>>>
>>>I'd suggest a two step approach, the first being to convert the binary string to an integer, then covert the integer to hex. I recently saw a routine to do the latter conversion, so here's the first part:
>>>
>>>FUNCTION BStr2Int
>>>
>>>LPARAMETER pc_bstr
>>>
>>>LOCAL lnresult, lni, lnj, lnlast
>>>lnlast = LEN(pc_bstr)
>>>lnj = -1
>>>lnresult = 0
>>>FOR lni = lnlast TO 1 STEP -1
>>> lnj = lnj + 1
>>> lnresult = lnresult + (VAL(SUBSTR(pc_bstr, lni, 1)) * (2 ^ lnj))
>>>NEXT
>>>RETURN lnresult
>>>
>>>Regards,
>>>
>>>George
>>
>>shouldn't even be that much work. every four places in binary will equal one hex 'digit'. have to read right to left, of course, so your example (1100 0011 1101 0001) would read B3C1. just 8 + 4 + 2 + 1 to the hex 'digit' for each group. hex to binary is just as easy.
>
>Dave,
>
>I suppose you could loop through in 4 character section increments, but at one point or another you're going to have come up with a value for the section, which means something like the above or:
>
>lnvalue = IIF(SUBSTR(lcstr, 1, 1) = 1, 8, 0) + IIF(SUBSTR(lcstr, 2, 1) = 1, 4, 0)...
>
>which is downright ugly, creates more overhead, and I would be astonished if it weren't much slower.
>
>What I like about the above is that the string passed doesn't have to be a specific length. You could even throw the four bit chucks to it and it work. Also it's very cohesive.
>
>BTW, you better make that C3D1.
>
>George

16 = 10hex. 15 = Ehex. 14 = Dhex
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform