Results 1 to 5 of 5

Thread: convert binary to Hex

  1. #1

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    convert binary to Hex

    How to do this without getting overflow errors?

    I need to convert 111111111111111111111111111

    to hex

    That is all

    Seahag

  2. #2
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    VB Code:
    1. Public Function BinaryToDecimal(BinaryString As String) As Long
    2.     If (Not Len(BinaryString) > 1023) Then
    3.         Dim retVal As Double
    4.         If (BinaryString <> "") And (Replace(Replace(BinaryString, "1", ""), "0", "") = "") Then
    5.             Dim i As Long
    6.             BinaryString = StrReverse(BinaryString)
    7.             For i = 0 To Len(BinaryString)
    8.                 If (Mid(BinaryString, i + 1, 1) = 1) Then
    9.                     retVal = retVal + (2 ^ (i))
    10.                 End If
    11.             Next i
    12.         End If
    13.         BinaryToDecimal = retVal
    14.     Else
    15.         MsgBox "Overflow would occur"
    16.         Exit Function
    17.     End If
    18. End Function
    19.  
    20. Private Sub Form_Load()
    21.     Dim strHex As String
    22.    
    23.     strHex = Hex(BinaryToDecimal("10000"))
    24. End Sub

    NOTE: the binaryTodecimal function is courtesy of plenderj
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  3. #3

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    That is gold..
    Thanks for clearing that up

  4. #4
    Hyperactive Member techman2553's Avatar
    Join Date
    Mar 2001
    Location
    <- To your left.
    Posts
    362
    The above code will convert a limited size Binary number (31 bits) to a Hex Number.

    If you want any size Binary Number to a Hex number, then try this...


    Code:
    Private Sub Form_Load()
      Debug.Print BinToHex("1100101100101100010101011010010101111010100101011010010100101100110101110111101010100101100101101001010")
    End Sub
    
    
    Private Function BinToHex(ByVal BinVal As String) As String
      Dim FourBits As String
      Dim BinValPosition As Integer
      Dim BitPosition As Integer
      Dim DigitValue As Integer
      
      BinVal = StrReverse(BinVal)
      
      For BinValPosition = 1 To Len(BinVal) Step 4
        FourBits = Mid(BinVal, BinValPosition, 4)
        DigitValue = 0
        For BitPosition = 1 To Len(FourBits)
          If Mid(FourBits, BitPosition, 1) = "1" Then
            DigitValue = DigitValue + 2 ^ (BitPosition - 1)
          End If
        Next BitPosition
        BinToHex = Mid("0123456789ABCDEF", DigitValue + 1, 1) & BinToHex
      Next BinValPosition
      
    End Function
    Last edited by techman2553; Mar 27th, 2002 at 02:26 PM.
    ----------

  5. #5

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    Cool thanks all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width