Other Number Bases, Hexadecimal, Octal, and Binary


OOo Basic provides the functions Hex and Oct to convert a number to hexadecimal and octal. No native support is provided for a conversion to and from binary. You can't directly use the output from Hex and Oct to convert the string back to a number because it is missing the leading "&H" and "&O".

 Print Hex(447)              '1BF Print CInt("&H" & Hex(747)) '747 Print Oct(877)              '1555 Print CInt("&O" & Oct(213)) '213 

The source code for Chapter 2 contains the function IntToBinaryString, which converts an integer to a binary number in the Operators module in the source code file SC02.sxw. The function is very flexible but it isn't particularly fast. A faster routine using the Hex function is shown in Listing 18 .

Listing 18: IntToBinaryString is found in the Numerical module in this chapter's source code files as SC03.sxw.
start example
 Function IntToBinaryString(ByVal x As Long) As String   Dim sHex As String   Dim sBin As String   Dim i As Integer   sHex = Hex(x)   For i=1 To Len(sHex)     Select Case Mid(sHex, i, 1)       Case "0"         sBin = sBin & "0000"       Case "1"         sBin = sBin & "0001"       Case "2"         sBin = sBin & "0010"       Case "3"         sBin = sBin & "0011"       Case "4"         sBin = sBin & "0100"       Case "5"         sBin = sBin & "0101"       Case "6"         sBin = sBin & "0110"       Case "7"         sBin = sBin & "0111"       Case "8"         sBin = sBin & "1000"       Case "9"         sBin = sBin & "1001"       Case "A"         sBin = sBin & "1010"       Case "B"         sBin = sBin & "1011"       Case "C"         sBin = sBin & "1100"       Case "D"         sBin = sBin & "1101"       Case "E"         sBin = sBin & "1110"       Case "F"         sBin = sBin & "1111"     End Select   Next   IntToBinaryString = sBin End Function 
end example
 

The code in Listing 18 may be long, but it's very simple. There is a correlation between hexadecimal digits and binary digits; each hexadecimal digit is composed of four binary digits. This relationship does not exist for base 10 numbers . The number is converted to a hexadecimal number using the Hex function. Each hexadecimal digit is converted to the corresponding binary digits. To convert a binary number in String form back to an Integer, use the code in Listing 19 .

Listing 19: BinaryStringToLong is found in the Numerical module in this chapter's source code files as SC03.sxw.
start example
 Function BinaryStringToLong(s$) As Long   Dim sHex As String   Dim sBin As String   Dim i As Integer   Dim nLeftOver As Integer   Dim n As Integer   n = Len(s$)   nLeftOver = n MOD 4   If nLeftOver > 0 Then     sHex = SmallBinToHex(Left(s$, nLeftOver))   End If   For i=nLeftOver + 1 To n Step 4     sHex = sHex & SmallBinToHex(Mid(s$, i, 4))   Next   BinaryStringToLong = CLng("&H" & sHex) End Function Function SmallBinToHex(s$) As String     If Len(s$) < 4 Then s$ = String(4-Len(s$), "0") & s$     Select Case s$       Case "0000"         SmallBinToHex = "0"       Case "0001"         SmallBinToHex = "1"       Case "0010"         SmallBinToHex = "2"       Case "0011"         SmallBinToHex = "3"       Case "0100"         SmallBinToHex = "4"       Case "0101"         SmallBinToHex = "5"       Case "0110"         SmallBinToHex = "6"       Case "0111"         SmallBinToHex = "7"       Case "1000"         SmallBinToHex = "8"       Case "1001"         SmallBinToHex = "9"       Case "1010"         SmallBinToHex = "A"       Case "1011"         SmallBinToHex = "B"       Case "1100"         SmallBinToHex = "C"       Case "1101"         SmallBinToHex = "D"       Case "1110"         SmallBinToHex = "E"       Case "1111"         SmallBinToHex = "F"     End Select End Function 
end example
 

To convert a binary string to an Integer, the number is first converted to a hexadecimal number. A set of four binary digits correspond to a single hexadecimal digit. The number is padded on the left with zeros so that the string can be broken up into blocks of four binary digits. Each block of four binary digits is converted to a single hexadecimal digit. The CLng function is then used to convert the hexadecimal number to decimal form. The routine in Listing 20 demonstrates the use of these functions; also see Figure 3 .

click to expand
Figure 3: Convert a whole number to hexadecimal, octal, and binary.
Listing 20: ExampleWholeNumberConversions is found in the Numerical module in this chapter's source code files as SC03.sxw.
start example
 Sub ExampleWholeNumberConversions   Dim s As String   Dim n As Long   Dim nAsHex$, nAsOct$, nAsBin$   s = InputBox("Number to convert:", "Long To Other", "1389")   If IsNull(s) Then Exit Sub   If Len(Trim(s)) = 0 Then Exit Sub   n = CLng(Trim(s))  'Trim removes leading and trailing spaces   nAsHex = Hex(n)   nAsOct = Oct(n)   nAsBin = IntToBinaryString(n)   s = "Original number = " & CStr(n) & CHR$(10) &_       "Hex(" & CStr(n) & ") = " & nAsHex & CHR$(10) &_       "Oct(" & CStr(n) & ") = " & nAsOct & CHR$(10) &_       "Binary(" & CStr(n) & " & nAsBin &_       " ==> " & BinaryStringToLong(nAsBin)   MsgBox(s, 0, "Whole Number Conversions") End Sub 
end example
 



OpenOffice.org Macros Explained
OpenOffice.org Macros Explained
ISBN: 1930919514
EAN: 2147483647
Year: 2004
Pages: 203

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net