Yet another feature request

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
dlh
Posts: 395
Joined: 15 December 2006, 12:12 PM
Location: ~Cincinnati

Yet another feature request

Post by dlh »

It would be nice to have a CStrBin(var) to convert a variable to a binary string.
stevech
Posts: 715
Joined: 22 February 2006, 20:56 PM

Post by stevech »

A binary ASCII string like "10101111" ?
pretty simple to code a function to do it.

rarely used?
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post 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
- Don Kinzer
Post Reply