Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Binary to Hex Conversion
Message
De
16/06/1997 10:32:47
 
 
À
15/06/1997 20:50:53
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00036266
Message ID:
00036508
Vues:
55
>>>VFP provides the BITSET() function to set a bit in a numeric value directly.
>>>
>>>There's no need to do all those conversions. Just work with numeric values.
>>>
>>>Vlad
>>>
>>>>>>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
>>>>
>>>>
>>>>Thanks George, I'll try it out. What I'm needing to do is transmit data to a remote server program. The remote program can accept optional fields i the middle of the string. The first field to send is a hex representation of the binary on/off (yes/no) of which fields are sent. For example, if field 5 is sent, then the 5th position in the binary number is set, etc.
>>>>
>>>>Craig
>>
>>Can you tell me how to do this, given my requirements?
>>
>>Craig
>
>To be fair, I didn't understand what you want to do exactly.
>What format do you have to convert in what format. When you say "binary number", is it a number in numeric format or a number in string format? The same for HEX: is it numeric HEX or string HEX?
>
>If both of them are numeric, than there may be nothing to convert. Altough, if you need to write them to a file you may need some conversions.
>
>Please tell us exactly what you need to do. What's the input, what's the output?
>
>You say that the binary format may be: 1100001111010001. Does it mean "1100001111010001"? Do you have actually 16 bytes there?
>
>For example, the HEX equivalent in string format is "C3D1", but the memory value in string format is chr(195)+chr(209).
>
>Vlad


It is a number in string format. Given a record with optional fields, each bit in the binary string corresponds to a field in the record. For example, if the fifth field is included, the fifth bit in the binary number is set on. If the fifth field is not included, the fifth bit in the binary number is set off. This binary number is then converted to hex and sent at the beginning of the record. When I get a reply record back, I have to do the opposite...convert from hex to binary to determine which fields have been sent. I may not always have 16 bits...it depends on the record type that is sent. Different record types have a different number of fields.

Craig
Craig Berntson
MCSD, Microsoft .Net MVP, Grape City Community Influencer
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform