Using Binary Coding
This is a demonstration of a piece of code that I have found useful called binary coding. Some time ago, I had a mailing list in which I had to associate each name with one or more organizations and needed to able to print mailing labels by one or a selection of organizations. I was using 20 different organizations but any number can be used. Rather than save the information for 20 organizations with each name, I decided to use binary coding converted to the decimal equivalent instead that allows only one number to have to be saved.
For example, if I set each bit on or off such as 00000000000000010011, the decimal equivalent would be 19. For those who can't convert from binary to decimal, starting from the right the decimal equivalent would be 1+2+16=19
The program has a series of check boxes which when checked will complete the following calculations.
Check box number 1 = 2^0 = 1 and add to the decimal equivalent Check box number 2 = 2^1 = 2 and add to the decimal equivalent - - Check box number 5 = 2^4 = 16 and add to the decimal equivalent +____ Decimal equivalent = 19
The program also contains a section that will take the decimal equivalent (called codenumber) and strip out the binary coding. As the binary coding is stripped out, the results can be saved into an array and used in a variety of ways. In the demo program it is used in a static text line to display the condition of the check boxes but could easily be used to set another set of check boxes instead or to determine if a name meets the requirements to be printed to a label.
The only thing one must be aware of is the length of the codenumber when all check boxes are set and to be sure to allow enough record length for that number when saving.
The Demonstration Code:
'Binary Coding Demo
nomainwin
WindowWidth = 350
WindowHeight = 365
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)
BackgroundColor$ = "buttonface"
ForegroundColor$ = "black"
checkbox #main.cb1, "Checkbox 1", [cb1Set], [cb1Reset],45,22,94,25
checkbox #main.cb2, "Checkbox 2", [cb2Set], [cb2Reset],45,47,94,25
checkbox #main.cb3, "Checkbox 3", [cb3Set], [cb3Reset],45,72,94,25
checkbox #main.cb4, "Checkbox 4", [cb4Set], [cb4Reset],45,97,94,25
checkbox #main.cb5, "Checkbox 5", [cb5Set], [cb5Reset],45,122,94,25
checkbox #main.cb6, "Checkbox 6", [cb6Set], [cb6Reset],45,147,94,25
checkbox #main.cb7, "Checkbox 7", [cb7Set], [cb7Reset],45,172,94,25
checkbox #main.cb8, "Checkbox 8", [cb8Set], [cb8Reset],45,197,94,25
checkbox #main.cb9, "Checkbox 9", [cb9Set], [cb9Reset],45,222,94,25
checkbox #main.cb10, "Checkbox 10", [cb10Set], [cb10Reset],45,247,101,25
statictext #main.statictext1, "", 180, 25, 160, 20
statictext #main.statictext2, "", 180, 50, 160, 20
statictext #main.statictext3, "", 180, 75, 160, 20
statictext #main.statictext4, "", 180, 100, 160, 20
statictext #main.statictext5, "", 180, 125, 160, 20
statictext #main.statictext6, "", 180, 150, 160, 20
statictext #main.statictext7, "", 180, 175, 160, 20
statictext #main.statictext8, "", 180, 200, 160, 20
statictext #main.statictext9, "", 180, 225, 160, 20
statictext #main.statictext10, "", 180, 250, 160, 20
statictext #main.statictext11, "", 90,310,170,20
button #main.button22,"Display Check Box Status",[button1Click],_
UL, 85, 280, 170, 25
open "Binary Coding Demo" for window as #main
print #main, "font arial 10"
print #main, "trapclose [quit]"
codenum=0
wait
'the formula for the code number is the
'sum of (2 raised to the power of each (check box minus 1))
[cb1Set] codenumber=codenumber+ 2^0: wait
[cb2Set] codenumber=codenumber+ 2^1: wait
[cb3Set] codenumber=codenumber+ 2^2: wait
[cb4Set] codenumber=codenumber+ 2^3: wait
[cb5Set] codenumber=codenumber+ 2^4: wait
[cb6Set] codenumber=codenumber+ 2^5: wait
[cb7Set] codenumber=codenumber+ 2^6: wait
[cb8Set] codenumber=codenumber+ 2^7: wait
[cb9Set] codenumber=codenumber+ 2^8: wait
[cb10Set] codenumber=codenumber+ 2^9: wait
[cb1Reset] codenumber=codenumber- 2^0: wait
[cb2Reset] codenumber=codenumber- 2^1: wait
[cb3Reset] codenumber=codenumber- 2^2: wait
[cb4Reset] codenumber=codenumber- 2^3: wait
[cb5Reset] codenumber=codenumber- 2^4: wait
[cb6Reset] codenumber=codenumber- 2^5: wait
[cb7Reset] codenumber=codenumber- 2^6: wait
[cb8Reset] codenumber=codenumber- 2^7: wait
[cb9Reset] codenumber=codenumber- 2^8: wait
[cb10Reset] codenumber=codenumber- 2^9: wait
[button1Click]'Take codenumber and strip out which
'check boxes are set or reset.
'The code number can be converted to a
'string and saved.
codenumber$=str$(codenumber)
code=codenumber
for x=9 to 0 step-1
y=2^x
if y>code then
array$(x+1)="reset"
else
code=code-y
array$(x+1)="set"
end if
next
'The following could also be used to set
'or reset another set of check boxes.
print #main.statictext1, "Checkbox 1 is "+array$(1)
print #main.statictext2, "Checkbox 2 is "+array$(2)
print #main.statictext3, "Checkbox 3 is "+array$(3)
print #main.statictext4, "Checkbox 4 is "+array$(4)
print #main.statictext5, "Checkbox 5 is "+array$(5)
print #main.statictext6, "Checkbox 6 is "+array$(6)
print #main.statictext7, "Checkbox 7 is "+array$(7)
print #main.statictext8, "Checkbox 8 is "+array$(8)
print #main.statictext9, "Checkbox 9 is "+array$(9)
print #main.statictext10, "Checkbox 10 is "+array$(10)
print #main.statictext11, "Code number is "+codenumber$
wait
[quit]
close #main
end