M8 Basics for VB (Basics.bas)
Basics for VB is a set of binary string and bit manipulation functions for VB5 / VB6. It contains the equivalent of DOS Basic's MKI$ CVI series for creating binary strings plus functions to check or set bits, to check or set bit fields and to check or set flags. There are also function to return the unsigned values of VB integer and long integers and functions to display byte, integer or long variables as a string of ones and zeros.
"Basic is the language of choice for logical programmers."
Why? - because it's logical - you can read a Basic listing and it makes sense! - Doesn't it? Well, usually. But when it comes to moving binary data between numeric variables and strings or bit manipulation its not so simple. Sure, it has a full set of bitwise operators, but unless you know your Hex numbers inside out, you're either going to need a good reference book or spend lots of time looking for example code on the Web.
With Basics.bas added to your project, you can forget about bitwise operators, masks, hex numbers and get back to writing logical readable code.
Here's a simple example:
The Windows API function VkKeyscan returns an integer in which the low byte is the virtual key code and the high byte has bit zero set if the Shift key is down, bit one set if the Ctrl key is down and bit two set if the Alt key is down.
You could figure it all out using AND with hex numbers, but wouldn't you rather write:
Return = VKKeyScan%(Char)
KeyCode = LowByte(Return)
Flags = HighByte(Return)
Shift = CheckBitInByte(0, Flags)
Ctrl = CheckBitInByte(1, Flags)
Alt = CheckBitInByte(2, Flags)
Have a look at the full function reference for Basics.bas below and see how much simpler VB can be.
The Demo Program
The demo is a compiled program which demonstrates some of the functions in Basics.bas. Although its primary purpose is, obviously, to help sell Basics.bas, it is also a useful program in its own right. You can enter a number, as signed, as unsigned, as hex or by setting bits or by setting flags. Whichever you enter, the other representations update - Including any designated bit field. It can, therefore, be a useful tool not only for debugging but also for teaching.

Note: Basics.bas is for VB5 / VB6. There is at present no .NET version.
Basics.bas Function Reference
Binary String Functions
Function IntegerToStr$(ByVal
Inn%)
Function LongToStr$(ByVal Inn&)
Function SingleToStr$(ByVal Inn!)
Function DoubleToStr$(ByVal Inn#)
Function CurrencyToStr$(ByVal Inn As Currency)
Param The variable to place in
a string
Return Value A Basic string containing the same bytes as the variable
These functions are the equivalent of the MKI$, MKL$, MKD$ functions from DOS
Basic
Function IntegerFromStr%(ByVal
Inn$)
Function LongFromStr&(ByVal Inn$)
Function SingleFromStr!(ByVal Inn$)
Function DoubleFromStr#(ByVal Inn$)
Function CurrencyFromStr(ByVal Inn$) As Currency
These functions retrieve variables from strings created with
the ToStr$ functions above
They are the equivalent of the CVI, CVL, CVD functions from DOS Basic
Integers translate to
a two byte string
Singles and Longs translate to a four byte string
Doubles and Currency translate to an eight byte string
An error occurs if you pass an invalid string length
Splitting and Joining Functions
Function LowByte(Inn%) As
Byte
Function HighByte(Inn%) As Byte
Function JoinBytes%(HighByte As Byte, Lowbyte As Byte)
Function LowWord%(Inn&)
Function HighWord%(Inn&)
Function JoinWords&(HighWord%, LowWord%)
Single Bit Manipulation
Function CheckBitInByte(Bitt%,
VarNo As Byte) As Boolean
Function CheckBitInInteger(Bitt%, VarNo%) As Boolean
Function CheckBitInLong(Bitt%, VarNo&) As Boolean
Param 1 The number of the bit
to check
Param 2 The variable to check in
Return Value True or False
Sub SetBitInByte(Bitt%, VarNo
As Byte)
Sub SetBitInInteger(Bitt%, VarNo%)
Sub SetBitInLong(Bitt%, VarNo&)
Param 1 The number of the bit
to set
Param 2 The variable to set it in
Sub ClearBitInByte(Bitt%,
VarNo As Byte)
Sub ClearBitInInteger(Bitt%, VarNo%)
Sub ClearBitInLong(Bitt%, VarNo&)
Param 1 The number of the bit
to clear
Param 2 The variable to clear it in
Sub ToggleBitInByte(Bitt%,
VarNo As Byte)
Sub ToggleBitInInteger(Bitt%, VarNo%)
Sub ToggleBitInLong(Bitt%, VarNo&)
Param 1 The number of the bit
to toggle
Param 2 The variable to toggle it in
Sub ReSetBitInByte(Bitt%,
VarNo As Byte, Action%)
Sub ReSetBitInInteger(Bitt%, VarNo%, Action%)
Sub ReSetBitInLong(Bitt%, VarNo&, Action%)
Param 1 The number of the bit
to change
Param 2 The variable to change it in
Param 3 The type of change
(0 or false = Clear, 1 or True = Set, Any other value = Toggle)
Multiple Bit Manipulation
The functions below use these user defined Types
Public Type IntegerBits
Bitt(0 To 15) As Boolean
End Type
Public Type LongBits
Bitt(0 To 31) As Boolean
End Type
Function IntegerToStructure(VarNo%)
As IntegerBits
Parameter The integer variable
to represent
Return Value An IntegerBits type in which the Bitt array represents the
bits in the variable
Function LongToStructure(VarNo&)
Parameter The long variable to
represent
Return Value A LongBits type in which the Bitt array represents the bits
in the variable
Function IntegerFromStructure%(Structure
As IntegerBits)
Parameter An IntegerBits Type
Return Value An integer in which the bits are set to the values in the
IntegerBits Type
Function LongFromStructure&(Structure
As LongBits)
Parameter A LongBits Type
Return Value A long integer in which the bits are set to the values in
the LongBits Type
Example Usage: To set bits 0 , 3 and 5 in xx%
Dim IntStruct as IntegerBits
IntStruct = IntegerToStructure(xx)
IntStruct.Bitt(0) = True
IntStruct.Bitt(3) = True
IntStruct.Bitt(5) = True
xx = IntegerFromStructure%(IntStruct)
Flag Functions
Function CheckFlagInbyte(Flag&,
VarNo As Byte) As Boolean
Function CheckFlagInInteger(Flag&, VarNo%) As Boolean
Function CheckFlagInLong(Flag&, VarNo&) As Boolean
Param 1 The flag to check for
Param 2 The variable to check for it in
Return Value True or False
Sub SetFlagInbyte(Flag&, VarNo
As Byte)
Sub SetFlagInInteger(Flag&, VarNo%)
Sub SetFlagInLong(Flag&, VarNo&)
Param 1 The flag to set
Param 2 The variable to set it in
Sub ClearFlagInbyte(Flag&,
VarNo As Byte)
Sub ClearFlagInInteger(Flag&, VarNo%)
Sub ClearFlagInLong(Flag&, VarNo&)
Param 1 The flag to clear
Param 2 The variable to clear it in
Unsigned Values
Function UnsignedLong@(LongValue&)
Function UnsignedInteger@(IntegerValue%)
These two functions return the unsigned values of Basic integers
and long integers in a Currency variable.
Bit Field Manipulation
Function BitFieldFromByte%(FirstBit%,
NoOfBits%, VarNo As Byte)
Function BitFieldFromInteger%(FirstBit%, NoOfBits%, VarNo%)
Function BitFieldFromLong%(FirstBit%, NoOfBits%, VarNo&)
Param 1 The first bit in the bit
field
Param 2 The number of bits in the bit field
Param 3 The variable to read from
Return Value The bit field value
Sub SetBitFieldInByte(FirstBit%,
NoOfBits%, FldValue%, VarNo As Byte)
Sub SetBitFieldInInteger(FirstBit%, NoOfBits%, FldValue%, VarNo%)
Sub SetBitFieldInLong(FirstBit%, NoOfBits%, FldValue%, VarNo&)
Param 1 The first bit in the bit
field
Param 2 The number of bits in the bit field
Param 3
The bit field value
Param 4 The variable to write the
bitfield into
Bit Display Functions
Function ByteToBits$(ByVal
VarNo As Byte)
Function IntegerToBits$(ByVal VarNo%)
Function LongToBits$(ByVal VarNo&)
Param The variable to represent
Return value A string representing the bits in the variable as ones and
zeros
e.g. "11010101 11100001"
Error Codes
Error 100 The String passed
to a "FromStr" function is the wrong length
Error 101 Bitt% is invalid for VarNo in the bit manipulation functions
Error 102 Invalid Flag sent to a Flag function
Error 103 Invalid parameters for a bit field function