Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting between binary number and integer 1

Status
Not open for further replies.

USMA99

Programmer
Oct 12, 2000
85
US
Is there a preexisting (therefore easy and fast) VFP function in which I can convert between a binary number such as 00001101 (which is the normal base10 number 13) and its integer value and vice versa?

Thanks
 
I'm unaware of anything built in for binary numbers. I wrote the following routines a while ago.

Rick

*** ------------------------------------------------------------------ ***
***
*** bins2int(str) : Binary String to Integer
*** int2bins(int [,int]) : Integer to Binary String
*** (Optional output length)
***
*** hexs2int(str) : Hex String to Integer
*** int2hexs(int [,int]) : Integer to Hex String
*** (Optional output length)
***
*** EXAMPLES:
***
*** ? bins2int("10000001010100010101")
*** ? int2bins(529685,20)
***
*** ------------------------------------------------------------------ ***

FUNCTION bins2int
PARAMETERS zbin_str
***
*** Assumes string of binary 'bits' no more than 32 characters long ***
*** Returns positive integer or -1 if in ERROR ***
***
PRIVATE lnumbits, lbit, ret_val, ii

ret_val = 0
zbin_str = alltrim(zbin_str) && kill leading and trailing spaces
DO WHILE LEFT(zbin_str,1) = "0" AND LEN(zbin_str) > 1 && kill leading "0"s
zbin_str = substr(zbin_str,2)
ENDDO
lnumbits = len(zbin_str)
IF lnumbits > 32 && too many bits for an integer
ret_val = -1
ELSE
FOR ii = 1 to lnumbits
lbit = substr(zbin_str, ii, 1)
DO CASE
CASE lbit = '0'
ret_val = 2*ret_val
CASE lbit = '1'
ret_val = 2*ret_val+1
OTHERWISE && NOT A '0' or '1'
ret_val = -1
EXIT
ENDCASE
ENDFOR
ENDIF
RETURN int(ret_val)

*** ------------------------------------------------------------------ ***

FUNCTION int2bins
PARAMETERS zdecimal, znoutlen
***
*** Assumes a positive integer less than 2^32 ***
*** Returns string of bits (0's or 1's) or 'ERROR: <explanation>' ***
***
PRIVATE lval, ret_val

IF PARAMETERS() < 2 OR TYPE('znoutlen') != 'N'
znoutlen = 0
ENDIF
znoutlen = MAX(0, MIN(32, znoutlen))

lval = min(2^32-1,abs(int(zdecimal)))
DO CASE
CASE lval = zdecimal
ret_val = ''
CASE lval = int(zdecimal)
ret_val = &quot;ERROR: Can't Handle Non-Integers&quot;
lval = 0
CASE lval = (-zdecimal)
IF zdecimal < 2^znoutlen-1
ret_val = ''
lval = MOD(-lval, 2^znoutlen)
ELSE
ret_val = &quot;ERROR: Can't Handle Negatives&quot;
lval = 0
ENDIF
OTHERWISE
ret_val = &quot;ERROR: Number greater than 2^32-1 (Too Large)&quot;
lval = 0
ENDCASE
DO while lval > 0
ret_val = iif((lval % 2)=1,'1','0')+ret_val
lval = int(lval/2)
ENDDO
IF znoutlen > 0 AND LEN(ret_val) < znoutlen
ret_val = PADL(ret_val, znoutlen, '0')
ENDIF
RETURN ret_val

*** ------------------------------------------------------------------ ***
 
Try looking up the help on these binary functions in VFP and see if they will do what you want.
BitAnd(), BitClear(), BitLShift(), BitNot(), BitOr(), BitRShift(), BitSet(), BitTest(), BitXor() David W. Grewe
Dave@internationalbid.com
ICQ VFP ActiveList #46145644
 
Another alternative to Rick's solution:

Code:
procedure BinToDec
lparameters tcBinary

lnRetVal=0
lnSeed=1
For X = Len(tcBinary) To 1 Step -1
	lnRetVal=lnRetVal+IIF(SUBSTR(tcBinary,X,1)='1',lnSeed,0)
	lnSeed=lnSeed*2
Next
return lnRetVal
endproc

The following Binary to Decimal function only returns a binary string beginning with the most significant ON bit. Example:

? DecToBin(16) &amp;&amp; returns '10000'

If the calling program needs a 8,16,32-bit,etc.. binary string, it's responsible for formatting. You can use the PADL() VFP function:

? PadL(DecToBin(16),8,'0') &amp;&amp; yields '00010000'

Code:
procedure DecToBin
lparameters tnDecimal

lnDecimal=tnDecimal
lcRetVal=''
lnSeed=1
do while lnSeed <= lnDecimal
	lnSeed=lnSeed*2
enddo
lnSeed=lnSeed/2
do while lnSeed >= 1
  If lnSeed <= lnDecimal
	  lnDecimal = lnDecimal-lnSeed
	  lcRetVal=lcRetVal+'1'
	else
	  lcRetVal=lcRetVal+'0'
	endif
  lnSeed=lnSeed/2
enddo
return lcRetVal
endproc
Jon Hawkins
jonscott8@yahoo.com

The World Is Headed For Mutiny,
When All We Want Is Unity. - Creed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top