Page 1 of 1

Yet another feature request

Posted: 29 April 2008, 13:49 PM
by dlh
It would be nice to have a CStrBin(var) to convert a variable to a binary string.

Posted: 29 April 2008, 20:59 PM
by stevech
A binary ASCII string like "10101111" ?
pretty simple to code a function to do it.

rarely used?

Posted: 03 May 2008, 9:15 AM
by dkinzer
stevech wrote:pretty simple to code a function to do it.
Here's an example for Byte values that is written to be easily modified to handle 16-bit or 32-bit values.

Code: Select all

Function CStrBinByte(ByVal val as Byte) as String
	Const bitCnt as Integer = SizeOf(val) * 8
	Dim buf(1 to bitCnt) as Byte
	Dim i as Integer
	For i = bitCnt to 1 Step -1
		buf(i) = IIF(CBool(LoByte(val) And 1), Asc("1"), Asc("0"))
		val = Shr(val, 1)
	Next i
	CStrBinByte = MakeString(buf.DataAddress, bitCnt)
End Function
Modified to handle Integer values:

Code: Select all

Function CStrBinInt(ByVal val as Integer) as String
	Const bitCnt as Integer = SizeOf(val) * 8
	Dim buf(1 to bitCnt) as Byte
	Dim i as Integer
	For i = bitCnt to 1 Step -1
		buf(i) = IIF(CBool(LoByte(val) And 1), Asc("1"), Asc("0"))
		val = Shr(val, 1)
	Next i
	CStrBinInt = MakeString(buf.DataAddress, bitCnt)
End Function