|
-
Mar 27th, 2002, 11:57 AM
#1
Thread Starter
Fanatic Member
convert binary to Hex
How to do this without getting overflow errors?
I need to convert 111111111111111111111111111
to hex
That is all
Seahag
-
Mar 27th, 2002, 12:01 PM
#2
Bouncy Member
VB Code:
Public Function BinaryToDecimal(BinaryString As String) As Long
If (Not Len(BinaryString) > 1023) Then
Dim retVal As Double
If (BinaryString <> "") And (Replace(Replace(BinaryString, "1", ""), "0", "") = "") Then
Dim i As Long
BinaryString = StrReverse(BinaryString)
For i = 0 To Len(BinaryString)
If (Mid(BinaryString, i + 1, 1) = 1) Then
retVal = retVal + (2 ^ (i))
End If
Next i
End If
BinaryToDecimal = retVal
Else
MsgBox "Overflow would occur"
Exit Function
End If
End Function
Private Sub Form_Load()
Dim strHex As String
strHex = Hex(BinaryToDecimal("10000"))
End Sub
NOTE: the binaryTodecimal function is courtesy of plenderj
-
Mar 27th, 2002, 12:41 PM
#3
Thread Starter
Fanatic Member
That is gold..
Thanks for clearing that up
-
Mar 27th, 2002, 02:22 PM
#4
Hyperactive Member
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.
----------
-
Mar 27th, 2002, 02:49 PM
#5
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|