|  |  |  Free VB Projects 
				
					|  | Convert integer numbers to binary string and vice versa |  Description:
			
			This VB code (functions) convert integer numbers to a binary string of the form '101010' and vice versa.
 
 Keywords:
			
			Binary numbers, binary numbering system, integer numebrs, long integer, decimal numbering system.
 
 
 
 
      Function IntToBin(ByVal IntegerNumber As Long)
      
          IntNum = IntegerNumber
          Do
              'Use the Mod operator to get the current binary digit from the
              'Integer number
              TempValue = IntNum Mod 2
              BinValue = CStr(TempValue) + BinValue
                      
              'Divide the current number by 2 and get the integer result
              IntNum = IntNum \ 2
          Loop Until IntNum = 0
          
          IntToBin = BinValue
          
      End Function
      
      Function BinToInt(ByVal BinaryNumber As String)
      
          'Get the length of the binary string
          Length = Len(BinaryNumber)
          'Convert each binary digit to its corresponding integer value
          'and add the value to the previous sum
          'The string is parsed from the right (LSB - Least Significant Bit)
          'to the left (MSB - Most Significant Bit)
          For x = 1 To Length
              TempValue = TempValue + Val(Mid(BinaryNumber, Length - x + 1, 1)) * 2 ^ (x - 1)
          Next
          
          BinToInt = TempValue
          
      End Function
      Return to VB projectsPrivate Sub Command1_Click()
          'Text1 contains the integer number. Text2 shows the binary result
          Text2.Text = IntToBin(Val(Text1.Text))
      End Sub
 Private Sub Command2_Click()
          'Text3 contains the binary string. Text4 shows the integer result
          Text4.Text = BinToInt(Text3.Text)
      End Sub
 |