How to do this without getting overflow errors?
I need to convert 111111111111111111111111111
to hex
That is all
Seahag
Printable View
How to do this without getting overflow errors?
I need to convert 111111111111111111111111111
to hex
That is all
Seahag
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 :)
That is gold..
Thanks for clearing that up :)
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
Cool thanks all
:)