<html><head><meta http-equiv=Content-Type content=text/html; charset=gb2312><title>g. 二/八/十/十六进制转换函数</title></head><body bgcolor=#F4ECDB><p align="center"><b><font size="5" color="#0000FF">g. 二/八/十/十六进制转换函数</font></b><p align="center"><font color="#FF0000">夜来香</font></p>Conversion Functions: Dec,Hex,Binary,Oct Back and Forth Here are a few conversion functions. Some of which have already been convered in threads, but I thought I would put them in an FAQ for easier reference. <br> 1 Hex>Decimal<br> Convert Hexadecimal values to Decimal<br> 2 Decimal>Hex<br> Convert Decimal values to Hexadecimal <br> 3 So. What if we need bigger? Different base?<br> 4 Dec2BasX <br> Convert Decimal values to any base from binary to hex (216)<br> 5 Binary>Decimal<br> Convert 1's and 0's to whole number<br> 6 Binary>Hex<br> Convert 1's and 0's to hex number<br> 7 Another Hex>Decimal<br> For grins, here's a manual hex>dec conversion function<br> 8 Float>Binary>BCD<br> Take a float (e.g. 123.45) and convert it to binary string which can then be converted to hex bcd using bin2hex. <br> 9 .Decimal>Binary<br> Convert floating point fraction to binary string<br> 10 Binary>Hexadecimal<br> Convert binary string to bcd hex in groups of 4<br> 11 BCD>Hexidecimal>Binary<br> Convert hex bcd number to binary representation of bcd number <br> 12 Binary>Float<br> Converts binary to floating point decimal number, whole portion and fraction.<br> 13 Binary>Decimal<br> Convert binary string to whole number<br> 14 Binary>.Decimal<br> Convert binary string to decimal fraction<br> 15 Tie 'em together <br> Let's do some converting now<br> 16 Complete procedure file CVT.PRG <br> This is a complete listing of the functions with no tutorials, so it should compile error free if you cut and paste to a procedure file.<br> 17 Form code to demo conversions <br> This is a demo form. Input a number and hit Enter or click the 'Convert' button. It will do the converting, then convert it back for comparison. Not that you need the aforementioned procedure file laying around so the program can grab it.<br> First, let's look at the built in ones.<br> 1 Hex>Decimal<br> Natively, VFP can do hex to decimal conversion like this:<br> ?0xffff<br> or<br> STORE 0xffff TO nVar <br> And so on. The result is an integer value, 65535.<br> 2 Decimal>Hex<br> VFP can also convert decimal to hex:<br> ?Transform(65535, '@0x')<br> or<br> STORE Transform(65535, '@0x') TO cHexStr<br> Notice that the return value is character, but you get a hex representation of 65535 as 0x0000FFFF.<br> Note: The largest hex value that can be returned by this method is 4294967295, or 0xFFFFFFFF<br> 3 So. What if we need bigger? Different base?<br> Following are a couple functions I wrote for previous apps. One function that will take a whole number in the range of 09007199254740992 and convert it to any base, as long as it is base 2 to 16. That includes binary, octal, or any useless base in between. <br> The biggest number it can handle is 9007199254740992, as VFP docs point out, that is the biggest number VFP can handle.<br> 4 Dec2BasX <br> *..............................................................................<br> * Function: DEC2BASX<br> * Purpose: Convert whole number 0?, to base 216 <br> *<br> * Parameters: nTempNum number to convert (09007199254740992)<br> * base base to convert to i.e., 2 4 8 16...<br> * returns: string<br> * Usage: cresult=Dec2BasX(nParm1, nParm2)<br> * STORE Dec2BasX(255, 16) TO cMyString &&... cMyString contains 'ff'<br> *..............................................................................<br> FUNCTION dec2basx<br> PARAMETERS nTempNum, nNewBase<br> STORE 0 TO nWorkVal,;<br> remainder,;<br> dividend,;<br> nextnum,;<br> digit<br> nWorkVal = nTempNum <br> ret_str = ''<br> DO WHILE .T.<br> digit = MOD(nWorkVal, nNewBase)<br> dividend = nWorkVal / nNewBase<br> nWorkVal = INT(dividend)<br> DO CASE<br> CASE digit = 10<br> ret_str = 'a' + ret_str<br> CASE digit = 11<br> ret_str = 'b' + ret_str<br> CASE digit = 12<br> ret_str = 'c' + ret_str<br> CASE digit = 13<br> ret_str = 'd' + ret_str<br> CASE digit = 14<br> ret_str = 'e' + ret_str<br> CASE digit = 15<br> ret_str = 'f' + ret_str<br> OTHERWISE<br> ret_str = LTRIM(STR(digit)) + ret_str<br> ENDCASE<br> IF nWorkVal = 0<br> EXIT<br> ENDIF ( nWorkVal = 0 )<br> ENDDO ( .T. )<br> RETURN ret_str<br> *: eof dec2basx<br> 5 Binary>Decimal<br> Now, we want to convert binary to decimal. This function will take a binary string between '0' and <br> '11111111111111111111111111111111111111111111111111111' <br> (53 1's, or 9007199254740991) and convert it to decimal. <br> *..................................................................<br> * Function: bin2dec<br> * Purpose: convert binary string to decimal number<br> * Parameters: pbinnum string to convert i.e.,<br> * '0' '11111111111111111111111111111111111111111111111111111' <br> * '0' (53 1's)<br> * returns: integer data type<br> * Usage: nresult = Bin2Dec(cParm1)<br> * STORE Bin2Dec('11111111') TO nMyNum &&... nMyNum contains 255 <br> *