Tuesday, August 14, 2018

Binary Conversion (String to binary then binary to decimal) in Visual Foxpro


Here are some useful function and procedures in Converting of STRING into BINARY then from BINARY to DECIMAL and vice versa.



*******************************************
* FUNCTION BIN2NUM( tcBinValue )
* Date       :  06-08-2002, 13:07:48
* author     : J. Wonzinski
*              Adjusted by B.L.P.M. Lutgerink
* description:
* convert binary format to numeric value
****** PARAMETERS **************
* Parameters :  1
* Parameter 1:  Binary string to convert to numeric value
*
* Example: Bin2Num( "00000111" ) -> 7
*******************************************
FUNCTION BIN2NUM( tcBinValue )

LOCAL lnReturn, lnPos, lnBit
lnReturn = 0

FOR lnPos = LEN(tcBinValue)  TO 1 STEP -1
 lnBit = VAL(SUBSTR(tcBinValue, lnPos, 1))
 lnReturn = lnReturn + lnBit * 2^(LEN(tcBinValue)-lnPos)
ENDFOR

RETURN INT(lnReturn)
ENDFUNC


*******************************************
* FUNCTION Num2Bin( tnNumtoConvert, tnStringLen )
* Date       : 06-08-2002, 13:07:48
* author     : J. Wonzinski
*              Adjusted by B.L.P.M. Lutgerink
* description:
* convert numeric value to binary format

****** PARAMETERS **************
* Parameters :  2
* Parameter 1:  Number to convert
* Parameter 2:  Length of the returnstring
*
*! Example: 7 is "111" in Binary format,  (7,8) returns "00000111"
*******************************************
FUNCTION Num2Bin( tnNumtoConvert, tnStringLen )

LOCAL lcBinValue
lcBinValue = ""

IF VARTYPE( tnStringLen ) <> "N" OR tnStringLen < 8
 * minimum length returned 8 bits.
 tnStringLen = 8
ENDIF

DO WHILE tnNumtoConvert > 0
 lcBinValue = STR(tnNumtoConvert % 2,1) + lcBinValue
 tnNumtoConvert  = FLOOR(tnNumtoConvert / 2)
ENDDO
IF VARTYPE(tnStringLen) = "N"
 lcBinValue = PADL(lcBinValue,tnStringLen,"0")
ENDIF

RETURN lcBinValue
ENDFUNC

*******************************************
* FUNCTION char2bin( tcChar )
* Date       :  15-12-2002, 15:35:44
* author     :  Boudewijn Lutgerink
* description:  convert character to binary value

****** PARAMETERS **************
* Parameters :  1
* Parameter 1:  character to convert
*
*******************************************
FUNCTION char2bin( tcChar )
RETURN Num2Bin( ASC(tcChar),8)
ENDFUNC

*******************************************
* FUNCTION str2bin( tcStringToConvert )
* Date       :  15-12-2002, 15:42:58
* author     :  Boudewijn Lutgerink
* description:  convert a string to binary values

****** PARAMETERS **************
* Parameters :  1
* Parameter 1:  string to convert
*
*******************************************
FUNCTION str2bin( tcStringToConvert )
LOCAL lcRetVal
lcRetVal = ""
FOR lnI = 1 TO LEN( tcStringToConvert )
 lcRetVal = lcRetVal + char2bin( SUBSTR(tcStringToConvert,lnI,1 ))
ENDFOR

RETURN lcRetVal
ENDFUNC

*******************************************
* FUNCTION binstr2str( tcBinStr )
* Date       :  15-12-2002, 17:31:59
* author     :  Boudewijn
* description:  convert a binary string to a readable string

****** PARAMETERS **************
* Parameters :  1
* Parameter 1:  binary-string to convert
*
*******************************************
FUNCTION binstr2str( tcBinStr )

LOCAL lcRetVal
lcRetVal = ""
FOR lnI = 0 TO (LEN(tcBinStr)-8)+1 STEP 8
 lcStr = SUBSTR(tcBinStr,lnI+1,8)
 lcRetVal = lcRetVal + CHR(BIN2NUM(lcStr))
ENDFOR

RETURN lcRetVal
ENDFUNC

}

No comments:

Post a Comment