EccentricDyslex Posted August 18, 2010 Posted August 18, 2010 (edited) Hi all, i have a problem with the code below. Basicaly i have an integer that i need to convert to binary and show the results in check boxes. The code below takes the integer, converts it to a binary string, then depending upon where the "1"s are, will check the apropriate check box on the UI. The binary number returned has a max of 16 digits and a min of 0, so hence the 16 checkboxes. The problem i am having is the code runs fine counting down from the left most "1" (int32768) with all the others to the right as "0"s. Halfing the int each cycle should result in the "1" moving along the checkboxes until it reaches the right most position. But its working fine down to box 9, but then it disapears! Any ideas? CODE- intInputs = 32768 'convert intinputs to binary strInputs = dec2baseN(intInputs, 2) 'find length of strInputs intLengthofinputsstring = Len(strInputs) 'find difference between strinputs and 16 so we know how many "0"s to add to the left of the string to keep everything in line Dim difbetweenstrinputsand16 As Integer difbetweenstrinputsand16 = 16 - Len(strInputs) 'if strInputs is less than 16 digits long, add 0s to the left of the string to make length always 16 digits long If difbetweenstrinputsand16 > 0 Then For a = 1 To difbetweenstrinputsand16 - 1 strInputs = "0" & strInputs Next End If 'clear all checkboxes CheckBox1.Checked = False CheckBox2.Checked = False CheckBox3.Checked = False CheckBox4.Checked = False CheckBox5.Checked = False CheckBox6.Checked = False CheckBox7.Checked = False CheckBox8.Checked = False CheckBox9.Checked = False CheckBox10.Checked = False CheckBox11.Checked = False CheckBox12.Checked = False CheckBox13.Checked = False CheckBox14.Checked = False CheckBox15.Checked = False CheckBox16.Checked = False 'analise the 16digit binary string and if any of the binary digits are ones, then check the related checkbox Dim temp As Integer For count = intLengthofinputsstring To 1 Step -1 temp = Microsoft.VisualBasic.Mid(strInputs, count, 1) If temp = 1 And count = 16 Then CheckBox1.Checked = True If temp = 1 And count = 15 Then CheckBox2.Checked = True If temp = 1 And count = 14 Then CheckBox3.Checked = True If temp = 1 And count = 13 Then CheckBox4.Checked = True If temp = 1 And count = 12 Then CheckBox5.Checked = True If temp = 1 And count = 11 Then CheckBox6.Checked = True If temp = 1 And count = 10 Then CheckBox7.Checked = True If temp = 1 And count = 9 Then CheckBox8.Checked = True If temp = 1 And count = 8 Then CheckBox9.Checked = True If temp = 1 And count = 7 Then CheckBox10.Checked = True If temp = 1 And count = 6 Then CheckBox11.Checked = True If temp = 1 And count = 5 Then CheckBox12.Checked = True If temp = 1 And count = 4 Then CheckBox13.Checked = True If temp = 1 And count = 3 Then CheckBox14.Checked = True If temp = 1 And count = 2 Then CheckBox15.Checked = True If temp = 1 And count = 1 Then CheckBox16.Checked = True Next 'divide intInputs by2 to move the "1" along by one step intInputs = intInputs / 2 then i loop this code 16 times. Any ideas? Cheers! Steve Edited August 18, 2010 by snarfblam Quote
Leaders snarfblam Posted August 18, 2010 Leaders Posted August 18, 2010 The first thing that jumps out at me is that you are comparing numbers to strings: temp = Microsoft.VisualBasic.Mid(strInputs, count, 1) If temp = 1 And count = 16 Then CheckBox1.Checked = True That kind of brings up the question of why you want to perform mathematical operations with strings anyways. The best way to scan through bits of an integer would be (IMO) to use bit shift and binary operators. To check the least significant bit of an integer, just AND its value with 1. Dim BinValue As Integer = GetValueFromThinAir() Dim lsbValue As Integer = BinValue And 1 If lsbValue = 1 Then ' first bit set Else ' first bit clear End If Now, you can scan through all the bits a couple of ways. One is to keep shifting the bits right. Dim BinValue As Integer = GetValueFromThinAir() ' Show message box with status of each bit For i As Integer = 0 To 15 Dim bitValue As Integer = BinValue And 1 Dim bitIsSet As Boolean = (bitValue = 1) MessageBox.Show("Bit " & i.ToString() & " = " & bitIsSet.ToString()) ' Sample output: "Bit 3 = False" ' Move to next bit BinValue = BinValue >> 1 Next i An alternative, if you want to check the bits in an arbitrary order, would be to create a bit filter for the bit you want to check, and, again, use the AND operator to check that bit. Dim BinValue As Integer = GetValueFromThinAir() Dim bitIndex = 5 ' Let's check bit # 5 Dim bitFilter As Integer = 1 << bitIndex Dim bitIsSet As Boolean = ((BinValue And bitFilter) = bitFilter) MessageBox.Show("Bit " & bitIndex.ToString() & " = " & bitIsSet.ToString()) ' Sample output: Bit 5 = True All the above is assuming you understand the relevant binary math. The other thing that stands out to me is the unrolled-bit-still-in-a-loop for loop. You have a loop, but that loop runs different code on each iteration, so why have a loop. Either make the loop purposeful, or fully unroll the loop. ' Unrolled (with hypothetical CheckBit function) CheckBox1.Checked = CheckBit(intInputs, 0) CheckBox2.Checked = CheckBit(intInputs, 1) CheckBox3.Checked = CheckBit(intInputs, 2) ' so on... ' Better loop (not sure about array syntax) Dim CheckBoxes() As New CheckBox() {CheckBox1, CheckBox2, Checkbox3} '... For i As Integer = 0 to CheckBoxes.Length - 1 CheckBoxes(i).Checked = CheckBit(intInputs, i) Next i Quote [sIGPIC]e[/sIGPIC]
EccentricDyslex Posted August 19, 2010 Author Posted August 19, 2010 Hi Snarf, i have found the original problem, so have it working. I am interested in your other method though, the integer is converted into a binary string by the function i found on the net. is it easy to turn that string into a binary number so the individual 1s and 0s can be analized? cheers steve Quote
Leaders snarfblam Posted August 19, 2010 Leaders Posted August 19, 2010 People tend to think of decimal, hex, binary, octal, etc. as different kinds of numbers. They aren't. They are different ways of representing the same exact kind of numbers. (Technically, all of your computer's data is processed in binary to begin with. The decimal values you see are just a different way of displaying the numbers to make it easier on us humans.) Converting numbers to strings to perform mathematical operations just doesn't make sense. Strings are for text. You just need to use the right kind of math on your integers. The purpose of the code I posted is to examine the individual ones and zeros. The fourth code listing in my post can examine any of the binary digits within an integer value. It would be easy to take the code and create a function that can be used the way I did in my last code listing. Quote [sIGPIC]e[/sIGPIC]
EccentricDyslex Posted August 20, 2010 Author Posted August 20, 2010 Thanks Snarf, works a treat! It was good a learing process to learn a bit about strings anyway. Basicaly an integer can be expresed in many ways i guess, and it is stored as bits anyway if that right? I had been looking all over the net looking for ways how to convert an int to a bin, but MS didnt have a function for this...now i see why...it already can be done by looking at bit values! I had wondered if there was an easier way of doing group operations like checking a number of check boxes the way i had...thats very useful, thanks! Steve Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.