Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
?0xffff
-or-
STORE 0xffff TO nVar
?Transform(65535, '@0x')
-or-
STORE Transform(65535, '@0x') TO cHexStr
*..............................................................................
* Function: DEC2BASX
* Purpose: Convert whole number 0-?, to base 2-16
*
* Parameters: nTempNum - number to convert (0-9007199254740992)
* base - base to convert to i.e., 2 4 8 16...
* returns: string
* Usage: cresult=Dec2BasX(nParm1, nParm2)
* STORE Dec2BasX(255, 16) TO cMyString &&... cMyString contains 'ff'
*..............................................................................
FUNCTION dec2basx
PARAMETERS nTempNum, nNewBase
STORE 0 TO nWorkVal,;
remainder,;
dividend,;
nextnum,;
digit
nWorkVal = nTempNum
ret_str = '
DO WHILE .T.
digit = MOD(nWorkVal, nNewBase)
dividend = nWorkVal / nNewBase
nWorkVal = INT(dividend)
DO CASE
CASE digit = 10
ret_str = 'a' + ret_str
CASE digit = 11
ret_str = 'b' + ret_str
CASE digit = 12
ret_str = 'c' + ret_str
CASE digit = 13
ret_str = 'd' + ret_str
CASE digit = 14
ret_str = 'e' + ret_str
CASE digit = 15
ret_str = 'f' + ret_str
OTHERWISE
ret_str = LTRIM(STR(digit)) + ret_str
ENDCASE
IF nWorkVal = 0
EXIT
ENDIF ( nWorkVal = 0 )
ENDDO ( .T. )
RETURN ret_str
*: eof dec2basx
*..................................................................
* Function: bin2dec
* Purpose: convert binary string to decimal number
* Parameters: pbinnum - string to convert i.e.,
* '0' - '11111111111111111111111111111111111111111111111111111'
* '0' - (53 1's)
* returns: integer data type
* Usage: nresult = Bin2Dec(cParm1)
* STORE Bin2Dec('11111111') TO nMyNum &&... nMyNum contains 255
*..................................................................
FUNCTION bin2dec
PARAMETERS pbinnum
PRIVATE retval, bindex
STORE 0 TO retval
pbinnum = Alltrim(pbinnum)
STORE Len(pbinnum) TO nDigits
FOR bindex = 0 TO nDigits
IF SUBSTR(pbinnum, nDigits - bindex, 1) = '1'
retval = retval + 2^bindex
ENDIF
NEXT
RETURN Int(retval)
*: eof bin2dec
?Dec2BasX(Bin2Dec('11111111'), 16) &&... returns 'ff'
-or-
STORE Dec2BasX(Bin2Dec('1111111111111111'), 16) TO cHexNum &&... cHexNum contains 'ffff'
?Transform(Bin2Dec('1111111111111111'), '@0x')
-or-
STORE Transform(Bin2Dec('1111111111111111'), '@0x') TO cHexNum
*..............................................................................
* Function: convert HEX STRING to decimal number
* i.e., '0001' returns 1 and
* 'ffff' returns 65535
* Parameters: hexnum - a string valued '0000' to 'ffff'
* decnum - initialized to 0 returns the value of the conversion
* Usage: nresult=hex2dec(cHexNum)
* STORE hex2dec('FFFF') TO nMyNum &&... nMyNum contains 65535
*..............................................................................
FUNCTION hex2dec
PARAMETERS hexnum
STORE 0 TO decnum
STORE ' TO tempnum
IF LEN(ALLTRIM(hexnum)) < 4
hexnum = PADL(hexnum, 4, '0')
ENDIF ( LEN(ALLTRIM(hexnum)) < 4 )
STORE LEN(hexnum)-1 TO indx
FOR i = 1 TO LEN(hexnum)
DO CASE
CASE SUBSTR(UPPER(hexnum), i, 1) == "F"
tempnum = '15'
CASE SUBSTR(UPPER(hexnum), i, 1) == "E"
tempnum = '14'
CASE SUBSTR(UPPER(hexnum), i, 1) == "D"
tempnum = '13'
CASE SUBSTR(UPPER(hexnum), i, 1) == "C"
tempnum = '12'
CASE SUBSTR(UPPER(hexnum), i, 1) == "B"
tempnum = '11'
CASE SUBSTR(UPPER(hexnum), i, 1) == "A"
tempnum = '10'
OTHERWISE
tempnum = SUBSTR(hexnum, i, 1)
ENDCASE
decnum = decnum + (VAL(tempnum) * (16^indx))
indx = indx - 1
ENDFOR ( i )
RETURN decnum
*: EOF hex2dec
*..............................................................................
* Function: float2bin
* Purpose: This function will take a float, 0.?, and convert it to binary
* representation which can then be converted to hex bcd using bin2hex.
*
* Parameters: nValue - float numer i.e., 123.45
* nWordSize: either 8 or 16 bit. 16 is default for better precision
* Calls: Dec2BasX() and Dec2Bin()
* returns: string
* Usage: cresult=float2bin(nValue)
* STORE float2bin(123.45) TO cMyBin
* cMyBin == '0100000001011110110111001100110011001100110011001100110011001101'
*..............................................................................
FUNCTION float2bin
PARAMETERS nValue, nWordSize
*... we'll default the wordsize to 16 if not specified.
IF Vartype(nWordSize) # 'N'
STORE 16 TO nWordSize
ENDIF
STORE IIF(nValue > 0, '0', '1') TO cSignBit
STORE 0 TO nExponent
STORE ABS(nValue) TO nCalcValue, nValue
STORE INT(nValue) TO nInt
IF nInt > 0
STORE MOD(nValue, INT(nValue)) TO nMantissa
ELSE
STORE nValue TO nMantissa
ENDIF
IF nInt > 1
cBinString = SUBSTR(dec2basx(nInt, 2) + dec2bin(nMantissa, nWordSize), 2)
ELSE
cBinString = dec2bin(nMantissa, nWordSize)
ENDIF
*... calc exponent
DO WHILE !(nCalcValue >= 1 AND nCalcValue < 2)
nCalcValue = nValue / (2 ^ nExponent) &&... start with +E
IF !(nCalcValue >= 1 AND nCalcValue < 2)
nExponent = nExponent * -1 &&... flip sign then try -E
nCalcValue = nValue / (2 ^ nExponent)
IF (nCalcValue >= 1 AND nCalcValue < 2)
EXIT
ENDIF
ELSE
EXIT
ENDIF
nExponent = ABS(nExponent) + 1 &&... keep going?
ENDDO
nMantissa = 1 + (1 / (2^nExponent))
IF nInt < 1
cBinString = SUBSTR(cBinString, ABS(nExponent) + 1)
ENDIF
IF nWordSize = 8
nBiased = 127
cExpBits = PADL(dec2basx(nExponent + nBiased, 2), 8, '0')
ELSE
nBiased = 1023
cExpBits = PADL(dec2basx(nExponent + nBiased, 2), 11, '0')
ENDIF
cBits = cSignBit + cExpBits + cBinString
IF nWordSize = 8
RETURN PADR(cBits, 32, '0')
ELSE
RETURN PADR(cBits, 64, '0')
ENDIF
*:EOF float2bin
*..............................................................................
* Function: dec2bin
* Purpose: convert floating point fraction to binary string -
* i.e. .75 = '11', .25 = '01' and .075 =
* '0001001100110011001100110011001100110011001100110011'
* Parameters: nDecimal - Float number i.e. .45
* nWordSize: either 8 or 16 bit. 16 is default for better precision
* returns: string data type
* Usage: nresult=dec2bin(nDecNum)
* STORE dec2bin(.45) TO nMyNum
* nMyNum == '0111001100110011001100110011001100110011001100110011'
*..............................................................................
FUNCTION dec2bin
PARAMETERS nDecimal, nWordSize
STORE ' TO cBinString
STORE nDecimal TO nWork
STORE 0 TO nCounter
*... we'll default the wordsize to 16 if not specified.
IF Vartype(nWordSize) # 'N'
STORE 16 TO nWordSize
ENDIF
IF nWordSize = 8
STORE 22 TO nMax
ELSE
STORE 51 TO nMax
ENDIF
DO WHILE nWork # 0 AND nCounter <= nMax &&... nMax precision bits
nWork = nWork * 2
IF nWork >= 1
cBinString = cBinString + '1'
nWork = MOD(nWork, INT(nWork))
ELSE
cBinString = cBinString + '0'
ENDIF
nCounter = nCounter + 1
ENDDO
RETURN cBinString
*: EOF dec2bin
*..............................................................................
* Function: bin2hex
* Purpose: convert binary representation of bcd number to hex
* Parameters: string data type. 123.45 =
* '0100000001011110110111001100110011001100110011001100110011001101'
* = 0x '405EDD2F1A9FBE77'
* Takes each segment of four binary digits and converts to hex digit
* Example: 0100 0000 0101 1110.... = 4 0 5 E .....
* returns: string data type
* Usage: cresult=bin2hex(cBinNum)
* STORE bin2hex('0100') TO nMyNum
* nMyNum == '4'
*..............................................................................
FUNCTION bin2hex
PARAMETERS cBinString
STORE ' TO cHexString
STORE Len(cBinString) TO nStrLen
FOR zzz = 1 TO nStrLen STEP 4
cHexString = cHexString + ;
dec2basx(bin2dec(SUBSTR(cBinString, zzz, 4)), 16)
NEXT
RETURN cHexString
*: EOF bin2hex
*..............................................................................
* Function: hex2bin
* Purpose: convert hex bcd number to binary representation of bcd number
* Parameters: string data type
* Takes each hex digit and converts to four binary digits
* Example: 4 = 0100, 0 = 0000, 5 = 0101 etc.
* returns: string data type
* Usage: cresult=hex2bin(cHexString)
* STORE hex2bin('405EDD2F1A9FBE77') TO nMyNum
* nMyNum == '0100000001011110110111010010111100011010100111111011111001110111'
*..............................................................................
FUNCTION hex2bin
PARAMETERS cHexString
STORE ' TO cBinString, cBinString1
STORE Len(cHexString) TO nStrLen
FOR zzz = 1 TO nStrLen
cBinString = cBinString + ;
Padl(dec2basx(Int(Val('0x' + Substr(cHexString, zzz, 1))), 2), 4, '0')
NEXT
RETURN cBinString
*: EOF hex2bin
*..............................................................................
* Function: bin2float
* Purpose: convert bcd binary to floating point decimal number
* Parameters: string data type of binary representation of bcd number
* '0100000001011110110111001100110011001100110011001100110011001101' = 123.45
* nWordSize: either 8 or 16 bit. 16 is default for better precision
* Calls: bin2dec() and bin2dec2()
* returns: string data type
* Usage: nresult=bin2float(cBinString)
* STORE bin2float('0111001100110011001100110011001100110011001100110011') ;
* TO nMyNum
* nMyNum == 123.45
*..............................................................................
FUNCTION bin2float
PARAMETERS cBinNum, nWordSize
IF Vartype(nWordSize) # 'N'
IF Len(cBinNum) = 32
STORE 8 TO nWordSize
ELSE &&... len better be 64
STORE 16 TO nWordSize
ENDIF
ENDIF
STORE IIF(LEFT(cBinNum, 1) = '1', '-', '+') + '1' TO cSign
IF nWordSize = 8
STORE SUBSTR(cBinNum, 2, 8) TO cExponent
STORE SUBSTR(cBinNum, 10) TO cMantissa
ELSE
STORE SUBSTR(cBinNum, 2, 11) TO cExponent
STORE SUBSTR(cBinNum, 13) TO cMantissa
ENDIF
IF nWordSize = 8
nExponent = bin2dec(cExponent) - 127
ELSE
nExponent = bin2dec(cExponent) - 1023
ENDIF
IF nExponent < 0
cDec = REPLICATE('0', ABS(nExponent)-1) + '1' + cMantissa
nDec = bin2dec2(cDec)
nInt = 0
ELSE
cInt = '1' + SUBSTR(cMantissa, 1, nExponent)
cDec = SUBSTR(cMantissa, nExponent + 1)
nInt = bin2dec(cInt)
nDec = bin2dec2(cDec)
ENDIF
nValue = nInt + nDec
nValue = nValue * VAL(cSign)
RETURN nValue
*: EOF bin2float
*..............................................................................
* Function: bin2dec
* Purpose: convert binary string to decimal number
* Parameters: pbinnum - string to convert i.e.,
* '0' - '11111111111111111111111111111111111111111111111111111'
* '0' - (53 1's)
* returns: integer data type
* Usage: nresult=bin2dec(cBinString)
* STORE bin2dec('11111111') TO nMyNum
* nMyNum == 255
*..............................................................................
FUNCTION bin2dec
PARAMETERS pbinnum
PRIVATE retval, bindex
STORE 0 TO retval
pbinnum = ALLTRIM(pbinnum)
STORE LEN(pbinnum) TO nDigits
FOR bindex = 0 TO nDigits
IF SUBSTR(pbinnum, nDigits - bindex, 1) = '1'
retval = retval + 2^bindex
ENDIF
NEXT
RETURN INT(retval)
*: eof bin2dec
*..............................................................................
* Function: bin2dec2
* Purpose: convert binary string to floating point fraction - i.e. .123
* Parameters: pbinnum - string to convert i.e.,
* '0' - '11111111111111111111111111111111111111111111111111' (50 1's)
* returns: float data type
* Usage: nresult=bin2dec2(cBinString)
* STORE bin2dec2('111') TO nMyNum
* nMyNum == .875
*..............................................................................
FUNCTION bin2dec2
PARAMETERS pbinnum
PRIVATE retval, bindex
STORE 0 TO retval
pbinnum = ALLTRIM(pbinnum)
STORE LEN(pbinnum) TO nDigits
FOR bindex = 1 TO nDigits
IF SUBSTR(pbinnum, bindex, 1) = '1'
retval = retval + 2^(-bindex)
ENDIF
NEXT
RETURN retval
*: eof bin2dec2