Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a decimal/hex/binary lookup table for the numbers 0 through 255
DescriptionThis example shows how to make a decimal/hex/binary lookup table for the numbers 0 through 255 in Visual Basic 6. The program loops through the values 0 through 255 printing their decimal, hexadecimal, and binary values into a file.
Keywordsdecimal, hex, binary, lookup, hexadecimal
CategoriesMiscellany, Algorithms
 
Thanks to Kenneth Ives.

Functions Get_Binary and Get_Hex return the binary and hexadecimal values for a decimal number, respectively.

 
Private Function Get_Hex(iDecValue As Integer) As String

' ---------------------------------------------------------
' Define variables
' ---------------------------------------------------------
  Dim sTmp As String

' ---------------------------------------------------------
' Convert the input value to Hex
' ---------------------------------------------------------
  sTmp = ""
  sTmp = Trim(Hex(iDecValue))

' ---------------------------------------------------------
' if after hex conversion, the length is only one
' char then prefix it with a zero
' ---------------------------------------------------------
  If Len(sTmp) < 2 Then sTmp = "0" & sTmp

' ---------------------------------------------------------
' Return the Hex character
' ---------------------------------------------------------
  Get_Hex = sTmp

End Function

Private Function Get_Binary(iInComing As Integer) As String

' ---------------------------------------------------------
' Written by Kenneth Ives             kenaso@home.com
'
' convert an integer value to its binary equivilent
'
' ---------------------------------------------------------

' ---------------------------------------------------------
' Define variables
' ---------------------------------------------------------
  Dim iDecValue As Integer
  Dim iBitCnt As Integer
  Dim iBitValue As Integer
  Dim iCurrBit As Integer
  Dim sTmp As String

' ---------------------------------------------------------
' Initialize variables
' ---------------------------------------------------------
  iDecValue = iInComing
  iBitCnt = 0
  iBitValue = 128
  sTmp = ""

' ---------------------------------------------------------
' Parse the incoming integer and determine which bits
' should be turned on
' ---------------------------------------------------------
  Do
      ' Increment the bit count
      iBitCnt = iBitCnt + 1

      ' divide the input integer by the current
      ' bit value.  Starts at 128
      iCurrBit = Int(iDecValue / iBitValue)

      ' if the results are greater than zero then
      ' a "1" is placed in that bit position; otherwise,
      ' a "0" will be placed there
      If iCurrBit > 0 Then
    
          ' append a "1" to the string
          sTmp = sTmp & "1"

          ' subtract the current bit value
          ' from the input integer
          iDecValue = iDecValue - iBitValue
      Else
          ' append a "0" to the string
          sTmp = sTmp & "0"
      End If
    
      ' Decrement the Bit value by dividing by 2
      iBitValue = Int(iBitValue / 2)
    
  Loop Until iBitCnt = 8

' ---------------------------------------------------------
' REturn the binary expression
' ---------------------------------------------------------
  Get_Binary = sTmp

End Function
 
The main program uses these routines to generate its output values.
 
Public Sub Main()

' ---------------------------------------------------------
' Decimal-Hex-Binary conversion table
'
' Written by Kenneth Ives             kenaso@home.com
'
' This module will create a two page text file in the root
' directory of drive C named Hex_Bin.txt.  Just load this
' module by itself and press F5 to execute.  Use any ASCII
' text editor to print the file.
' ---------------------------------------------------------

' ---------------------------------------------------------
' Define variables
' ---------------------------------------------------------
  Dim i As Integer
  Dim k As Integer
  Dim iFile As Integer     ' file handle
  Dim iChar As Integer
  Dim sHChar As String     ' Hex character
  Dim sBChar As String     ' Binary character
  Dim sDChar As String     ' Decimal character
  Dim sHeader1 As String   ' Header line 1
  Dim sheader2 As String   ' Header line 2
  
  Dim sPrnOut(0 To 35) As String
  
  Const sHexBin = "C:\Hex_Bin.txt"
  Const TITLE1 = "Dec Hex    Binary"
  Const TITLE2 = "-----------------"
  Const m = 3
  
' ---------------------------------------------------------
' Initialize variables
' ---------------------------------------------------------
  sHeader1 = TITLE1 & Space(m) & TITLE1 & Space(m) & TITLE1 _
      & Space(m) & TITLE1
  sheader2 = TITLE2 & Space(m) & TITLE2 & Space(m) & TITLE2 _
      & Space(m) & TITLE2
  
' ---------------------------------------------------------
' do the first page
' ---------------------------------------------------------
  Erase sPrnOut                   ' empty the array
  k = 0                           ' set index to zero
  
  ' process ASCII decimal values 0 to 127 first
  For i = 0 To 127
      iChar = i
      sHChar = Get_Hex(iChar)      ' convert to Hex
      sBChar = Get_Binary(iChar)   ' convert to Binary
      
      If iChar > 95 Then   ' column 4, page 1
          sDChar = Trim(CStr(iChar))
          If Len(sDChar) = 2 Then sDChar = " " & sDChar  '
              ' pad blanks if needed
          
          ' Build the string in the array
          sPrnOut(k) = sPrnOut(k) & Space(m) & sDChar & _
              Space(2) & sHChar & Space(2) & sBChar
          
          ' If at bottom of column, then reset.  Otherwise
          ' increment.
          If k = 31 Then k = 0 Else k = k + 1

      ElseIf iChar > 63 Then   ' column 3, page 1
          sDChar = Trim(CStr(iChar))
          If Len(sDChar) = 2 Then sDChar = " " & sDChar  '
              ' pad blanks if needed
          
          ' Build the string in the array
          sPrnOut(k) = sPrnOut(k) & Space(m) & sDChar & _
              Space(2) & sHChar & Space(2) & sBChar
          
          ' If at bottom of column, then reset.  Otherwise
          ' increment.
          If k = 31 Then k = 0 Else k = k + 1

      ElseIf iChar > 31 Then   ' column 2, page 1
          sDChar = Trim(CStr(iChar))
          If Len(sDChar) = 2 Then sDChar = " " & sDChar  '
              ' pad blanks if needed
          
          ' Build the string in the array
          sPrnOut(k) = sPrnOut(k) & Space(m) & sDChar & _
              Space(2) & sHChar & Space(2) & sBChar
          
          ' If at bottom of column, then reset.  Otherwise
          ' increment.
          If k = 31 Then k = 0 Else k = k + 1

      Else   ' column 1, page 1
          sDChar = Trim(CStr(iChar))
          
          ' pad blanks if needed
          If Len(sDChar) = 1 Then
              sDChar = Space(2) & sDChar
          ElseIf Len(sDChar) = 2 Then
              sDChar = " " & sDChar
          End If
          
          ' Build the string in the array
          sPrnOut(k) = sDChar & Space(2) & sHChar & _
              Space(2) & sBChar
          
          ' If at bottom of column, then reset.  Otherwise
          ' increment.
          If k = 31 Then k = 0 Else k = k + 1
      End If
  Next
  
' ---------------------------------------------------------
' Write the page one data to C:\Bin_Hex.txt file
' ---------------------------------------------------------
  iFile = FreeFile                    ' get first free file
      ' handle
  Open sHexBin For Output As #iFile   ' Open a new copy of
      ' the file
  
  For k = 1 To 7                      ' 7 blank lines
      Print #iFile, " "
  Next
  
  Print #iFile, sHeader1              ' Column titles
  Print #iFile, sheader2              ' Column title
      ' separator line
  
  For k = 0 To 31
      Print #iFile, sPrnOut(k)        ' All four columns of
          ' data
  Next
  
  Print #iFile, Chr(12)               ' Page break on a
      ' separate line
  
' ---------------------------------------------------------
' do the second page
' ---------------------------------------------------------
  Erase sPrnOut                   ' empty the array
  k = 0                           ' set index to zero
  
  ' process ASCII decimal values 128 to 255 next
  For i = 128 To 255
      iChar = i
      sHChar = Get_Hex(iChar)      ' convert to Hex
      sBChar = Get_Binary(iChar)   ' convert to Binary
      
      If iChar > 223 Then  ' column 4, page 2
          sDChar = Trim(CStr(iChar))
          
          ' Build the string in the array
          sPrnOut(k) = sPrnOut(k) & Space(m) & sDChar & _
              Space(2) & sHChar & Space(2) & sBChar
          
          ' If at bottom of column, then reset.  Otherwise
          ' increment.
          If k = 31 Then k = 0 Else k = k + 1

      ElseIf iChar > 191 Then  ' column 3, page 2
          sDChar = Trim(CStr(iChar))
          
          ' Build the string in the array
          sPrnOut(k) = sPrnOut(k) & Space(m) & sDChar & _
              Space(2) & sHChar & Space(2) & sBChar
          
          ' If at bottom of column, then reset.  Otherwise
          ' increment.
          If k = 31 Then k = 0 Else k = k + 1

      ElseIf iChar > 159 Then  ' column 2, page 2
          sDChar = Trim(CStr(iChar))
          
          ' Build the string in the array
          sPrnOut(k) = sPrnOut(k) & Space(m) & sDChar & _
              Space(2) & sHChar & Space(2) & sBChar
          
          ' If at bottom of column, then reset.  Otherwise
          ' increment.
          If k = 31 Then k = 0 Else k = k + 1

      Else ' column 1, page 2
          sDChar = Trim(CStr(iChar))
          
          ' Build the string in the array
          sPrnOut(k) = sDChar & Space(2) & sHChar & _
              Space(2) & sBChar
          
          ' If at bottom of column, then reset.  Otherwise
          ' increment.
          If k = 31 Then k = 0 Else k = k + 1
      End If
  Next
  
' ---------------------------------------------------------
' Write the page two data to C:\Bin_Hex.txt file
' ---------------------------------------------------------
  For k = 1 To 7                      ' 7 blank lines
      Print #iFile, " "
  Next
  
  Print #iFile, sHeader1              ' Column titles
  Print #iFile, sheader2              ' Column title
      ' separator line
  
  For k = 0 To 31
      Print #iFile, sPrnOut(k)        ' All four columns of
          ' data
  Next
  
  Print #iFile, " "                   ' make sure EOF is on
      ' separate line
  Close iFile                         ' close the file
  End                                 ' Terminate program
  
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated