Dear guys
I am trying to convert huge hex number to dec in Vb6 but I couldn't manage .
I found one function which it could convert 23 hex number to dec but I want to convert more for example 300 or even more.
I would like to ask you guys if you have source code for that ,please post it here .
Thank you .
You can convert hex to decimal using the Chr$() function:
MsgBox Chr$("&H20")
What do you mean converting a hex value of "300 or more"? Do you mean one really long hex value or a bunch of different ones?
Yes ,I mean 0ne really long hex number.For example one hex number with 300 digits.
And thanks to the other guys ,I am just checking them now.
That was really fast answers.
I checked all of them but unfortunatly as I said they can't convert a one huge hex to dec.
They are just able to convert the small ones(Max 16 digits)But I want more.
Thank You
Option Explicit
Public Function BinaryToDecimal(Binary As String) As Long
Dim n As Long
Dim s As Integer
For s = 1 To Len(Binary)
n = n + (Mid(Binary, Len(Binary) - s + 1, 1) * (2 ^ _
(s - 1)))
Next s
BinaryToDecimal = n
End Function
Public Function DecimalToBinary(DecimalNum As Long) As _
String
Dim tmp As String
Dim n As Long
n = DecimalNum
tmp = Trim(Str(n Mod 2))
n = n \ 2
Do While n <> 0
tmp = Trim(Str(n Mod 2)) & tmp
n = n \ 2
Loop
DecimalToBinary = tmp
End Function
Public Function HextoBinary(HexVal As String) As String
Dim binVal As String
If Len(HexVal) = 1 Then
binVal = Hex_MAP_BIN(HexVal)
If binVal <> "" Then
HextoBinary = binVal
Debug.Print HexVal & "-" & binVal
Else
Exit Function
End If
Else
Debug.Print Mid(HexVal, Len(HexVal), 1)
Debug.Print Mid(HexVal, 1, Len(HexVal) - 1)
binVal = HextoBinary(Mid(HexVal, 1, Len(HexVal) - 1)) & HextoBinary(Mid(HexVal, Len(HexVal), 1))
HextoBinary = binVal
End If
End Function
Public Function Hex_MAP_BIN(HexVal As String) As String
Select Case UCase(HexVal)
Case "0"
Hex_MAP_BIN = "0000"
Case "1"
Hex_MAP_BIN = "0001"
Case "2"
Hex_MAP_BIN = "0010"
Case "3"
Hex_MAP_BIN = "0011"
Case "4"
Hex_MAP_BIN = "0100"
Case "5"
Hex_MAP_BIN = "0101"
Case "6"
Hex_MAP_BIN = "0110"
Case "7"
Hex_MAP_BIN = "0111"
Case "8"
Hex_MAP_BIN = "1000"
Case "9"
Hex_MAP_BIN = "1001"
Case "A"
Hex_MAP_BIN = "1010"
Case "B"
Hex_MAP_BIN = "1011"
Case "C"
Hex_MAP_BIN = "1100"
Case "D"
Hex_MAP_BIN = "1101"
Case "E"
Hex_MAP_BIN = "1110"
Case "F"
Hex_MAP_BIN = "1111"
Case Else
End Select
End Function
Public Function HexToDecimal(HexVal As String) As Long
Dim binVal As String
Dim decVal As Long
binVal = HextoBinary(HexVal)
If binVal = "01011110101101010010" Then MsgBox "OK"
decVal = BinaryToDecimal(binVal)
HexToDecimal = decVal
End Function
in a command button
Code:
Private Sub Command2_Click()
MsgBox HexToDecimal("5EB52")
End Sub
Yes ,I mean 0ne really long hex number.For example one hex number with 300 digits.
And thanks to the other guys ,I am just checking them now.
That was really fast answers.
A hex number with 300 digits is so large that it isn't a meaningful number any longer; there is absolutely nothing useful you can do with it.
To put it in perspective, Planck's constant is the measure below which you cannot observe without disturbing what you're observing because it's smaller than the wavelength of light. ie: Sub-atomic particles. Planck units also apply to time; you can think of it as an instant of time so short that it is indivisible; a true "moment".
The four-dimensional space-time volume of the history of the known universe -- the spatial volume in unimaginably small units * the age in indivisibly small increments -- is on the scale of 10^245. And you're asking about 16^300.
Let us know if you manage to do it, because if you do then we can start exhaustively enumerating every moment in the life of every subatomic particle that has ever existed in the history of the universe.
Option Explicit
Public Function BinaryToDecimal(Binary As String) As Long
Dim n As Long
Dim s As Integer
For s = 1 To Len(Binary)
n = n + (Mid(Binary, Len(Binary) - s + 1, 1) * (2 ^ _
(s - 1)))
Next s
BinaryToDecimal = n
End Function
Public Function DecimalToBinary(DecimalNum As Long) As _
String
Dim tmp As String
Dim n As Long
n = DecimalNum
tmp = Trim(Str(n Mod 2))
n = n \ 2
Do While n <> 0
tmp = Trim(Str(n Mod 2)) & tmp
n = n \ 2
Loop
DecimalToBinary = tmp
End Function
Public Function HextoBinary(HexVal As String) As String
Dim binVal As String
If Len(HexVal) = 1 Then
binVal = Hex_MAP_BIN(HexVal)
If binVal <> "" Then
HextoBinary = binVal
Debug.Print HexVal & "-" & binVal
Else
Exit Function
End If
Else
Debug.Print Mid(HexVal, Len(HexVal), 1)
Debug.Print Mid(HexVal, 1, Len(HexVal) - 1)
binVal = HextoBinary(Mid(HexVal, 1, Len(HexVal) - 1)) & HextoBinary(Mid(HexVal, Len(HexVal), 1))
HextoBinary = binVal
End If
End Function
Public Function Hex_MAP_BIN(HexVal As String) As String
Select Case UCase(HexVal)
Case "0"
Hex_MAP_BIN = "0000"
Case "1"
Hex_MAP_BIN = "0001"
Case "2"
Hex_MAP_BIN = "0010"
Case "3"
Hex_MAP_BIN = "0011"
Case "4"
Hex_MAP_BIN = "0100"
Case "5"
Hex_MAP_BIN = "0101"
Case "6"
Hex_MAP_BIN = "0110"
Case "7"
Hex_MAP_BIN = "0111"
Case "8"
Hex_MAP_BIN = "1000"
Case "9"
Hex_MAP_BIN = "1001"
Case "A"
Hex_MAP_BIN = "1010"
Case "B"
Hex_MAP_BIN = "1011"
Case "C"
Hex_MAP_BIN = "1100"
Case "D"
Hex_MAP_BIN = "1101"
Case "E"
Hex_MAP_BIN = "1110"
Case "F"
Hex_MAP_BIN = "1111"
Case Else
End Select
End Function
Public Function HexToDecimal(HexVal As String) As Long
Dim binVal As String
Dim decVal As Long
binVal = HextoBinary(HexVal)
If binVal = "01011110101101010010" Then MsgBox "OK"
decVal = BinaryToDecimal(binVal)
HexToDecimal = decVal
End Function
in a command button
Code:
Private Sub Command2_Click()
MsgBox HexToDecimal("5EB52")
End Sub
tel me if this works
Unfortunatly NO!
If you want to test it insted of MsgBox HexToDecimal("5EB52")
Put MsgBox HexToDecimal("aaaaaaaaaa")
And you will see it's gonna give you the overflow error.
Decimal is bigger than long, but it's not a natice type; it's a variant sub-type. Currency and double are also bigger than Long, but Decimal is the biggest.
A hex number with 300 digits is so large that it isn't a meaningful number any longer; there is absolutely nothing useful you can do with it.
To put it in perspective, Planck's constant is the measure below which you cannot observe without disturbing what you're observing because it's smaller than the wavelength of light. ie: Sub-atomic particles. Planck units also apply to time; you can think of it as an instant of time so short that it is indivisible; a true "moment".
The four-dimensional space-time volume of the history of the known universe -- the spatial volume in unimaginably small units * the age in indivisibly small increments -- is on the scale of 10^245. And you're asking about 16^300.
Let us know if you manage to do it, because if you do then we can start exhaustively enumerating every moment in the life of every subatomic particle that has ever existed in the history of the universe.
Please be more clear.
You mean there is no way to calculate that number(Which I belive there is) or you mean there is a way but That number is not useful!
I am waiting for your answer because if there is not I have to think about something else.
Unfortunatly NO!
If you want to test it insted of MsgBox HexToDecimal("5EB52")
Put MsgBox HexToDecimal("aaaaaaaaaa")
And you will see it's gonna give you the overflow error.
well no, this is just an example.
of course there is a limit, you know that data types have limits.
I made a change
do it and see
Code:
Public Function BinaryToDecimal(Binary As String) As Double
Dim n As Double
Dim s As Long
For s = 1 To Len(Binary)
n = n + (Mid(Binary, Len(Binary) - s + 1, 1) * (2 ^ _
(s - 1)))
Next s
BinaryToDecimal = n
End Function
Code:
Public Function HexToDecimal(HexVal As String) As Double
Dim binVal As String
Dim decVal As Double
binVal = HextoBinary(HexVal)
If binVal = "01011110101101010010" Then MsgBox "OK"
decVal = BinaryToDecimal(binVal)
HexToDecimal = decVal
End Function
and the command goes
Code:
Private Sub Command2_Click()
MsgBox HexToDecimal("2134325423423452354765785609234defaffbc9049823984908234")
End Sub
Pretending for the moment that we're talking about base 10 instead of base 16:
Currency tops out at ~ 10^15 (922,337,203,685,477)
Decimal tops out at ~ 10^29 (79,228,162,514,264,337,593,543,950,335)
Double can handle ~ 10^308 (1.79769313486232E308), but it only maintains just over a dozen significant digits.
Please be more clear.
You mean there is no way to calculate that number(Which I belive there is) or you mean there is a way but That number is not useful!
I am waiting for your answer because if there is not I have to think about something else.
You can't treat it as a number. You will have to write your own library from scratch to do it. Such a library will have to handle the numbers as strings, and you'll need to code all mathematical operations the long way, just like we all learned in elementary school.
"80256321532" +
"65487213654" =
4+2=6
5+3=8
6+5=1 (carry the 1!)
3+1=4+1=5
1+2=3
3+2=5
6+7=3 (carry the 1!)
8+5=3+1=4 (carry the 1!)
4+2=6+1=7
5+0=5
6+8=4 (carry the 1!)
=
145743535186
That's the easy part, of course. Multiplication and long division will be a mess, and god help you if you need to do something crazy like multiply by an irrational number or find the square root.
well no, this is just an example.
of course there is a limit, you know that data types have limits.
I made a change
do it and see
Code:
Public Function BinaryToDecimal(Binary As String) As Double
Dim n As Double
Dim s As Long
For s = 1 To Len(Binary)
n = n + (Mid(Binary, Len(Binary) - s + 1, 1) * (2 ^ _
(s - 1)))
Next s
BinaryToDecimal = n
End Function
Code:
Public Function HexToDecimal(HexVal As String) As Double
Dim binVal As String
Dim decVal As Double
binVal = HextoBinary(HexVal)
If binVal = "01011110101101010010" Then MsgBox "OK"
decVal = BinaryToDecimal(binVal)
HexToDecimal = decVal
End Function
and the command goes
Code:
Private Sub Command2_Click()
MsgBox HexToDecimal("2134325423423452354765785609234defaffbc9049823984908234")
End Sub
i ll put the output
Man you are the best .
I owe you one.
It's working Perfectly.
You can't understand how happy I am .
Thaaaaaaaaaank You very much!
well , I tried with MsgBox HexToDecimal("aaaaaaaaaaaaaaaaa")
here is the output again. DigiRev I gave only a method or an Algorithm for you to try. you can use it with any laguage and any data type. Thats up to you. First you asked for 300 Hex values , but this is closer i guess.
Check THIS also
You can't treat it as a number. You will have to write your own library from scratch to do it. Such a library will have to handle the numbers as strings, and you'll need to code all mathematical operations the long way, just like we all learned in elementary school.
"80256321532" +
"65487213654" =
4+2=6
5+3=8
6+5=1 (carry the 1!)
3+1=4+1=5
1+2=3
3+2=5
6+7=3 (carry the 1!)
8+5=3+1=4 (carry the 1!)
4+2=6+1=7
5+0=5
6+8=4 (carry the 1!)
=
145743535186
That's the easy part, of course. Multiplication and long division will be a mess, and god help you if you need to do something crazy like multiply by an irrational number or find the square root.
It's look like you know math perfectly.
But as you can see our freind Zeezee just gave the source code and it's working perfectly.
You mean this number is not the actuall number which I expected to have.
by the way I will test it in my way and let you know and thank you very much for your useful information ,I think I have to research more about the things you just mentioned.
Yeeeeeeeeeeeeeeeeee ... ha ...
but this may have limitations, be careful to check for overflows.
and there is a loop also
I m happy . because you are happy.
You can't treat it as a number. You will have to write your own library from scratch to do it. Such a library will have to handle the numbers as strings, and you'll need to code all mathematical operations the long way, just like we all learned in elementary school.
"80256321532" +
"65487213654" =
4+2=6
5+3=8
6+5=1 (carry the 1!)
3+1=4+1=5
1+2=3
3+2=5
6+7=3 (carry the 1!)
8+5=3+1=4 (carry the 1!)
4+2=6+1=7
5+0=5
6+8=4 (carry the 1!)
=
145743535186
That's the easy part, of course. Multiplication and long division will be a mess, and god help you if you need to do something crazy like multiply by an irrational number or find the square root.
You mean this number is not the actuall number which I expected to have.
It is not even remotely close to the number. It only has 15 significant digits. To put that into context for you, throw this function into that module and run it:
Code:
Public Sub Imprecision()
Dim dblNumber As Double
Dim dblCompare As Double
dblNumber = HexToDecimal("2134325423423452354765785609234defaffbc9049823984908234")
dblCompare = dblNumber - 10000000000000#
If dblNumber = dblCompare Then
MsgBox "x minus 10,000,000,000,000 is equal to x"
End If
End Sub
This, on the other hand, is exactly what you're looking for.
Yep , you are right. This is just what you want.
Its a bit similar to what I did, like first convert to base2 then to base10, but this is better.
I did this one by looking into that wiki article. I see that this method uses just the same method to convert from base to base using the algorithm given there.
I tested both codes, they work fine, my one seems bit speadier though.
test the both.
This, on the other hand, is exactly what you're looking for.
Did you test this one?
I just test it but it's look like it will not give you the correct answer.
Just test it with 10 "a" and select from base 16 to base 10 ,then copy the result and change the setting vise versa ,it won't give you the correct answer.
If I am wrong please let me know
Ok I took Screen Shots as u can see the first pic is convert 4 "a" from 16 to 10 and I copied the result and tried to convert that number from base 10 to base 16 ,but you see the result.
What's wrong?
Am I doing smt wrong?
I don't really understand hex too well, but my guess is that hex "aaaa" is a negative number, which isn't supported in that program. (Neither are decimals.) I only say aaaa might be a negative number because of the debug window, but I'm probably doing it wrong:
Got it!
You have to enter your alphabet as capital!
So if you put "AAAAA" and vise versa it's gonna work .
I don't know how to say thanks to you guys ,but I really apreciate your help and I hope I can help you in the future.
But I am not finish ,for sure I will come back with other question.
Especial thanks to AAAAAAAALLLLLLLLLLLLLLLLLL!
I would imagine that it shouldn't be too difficult to create a function that would take a hexidecimal string of arbitrary length and return a decimal string of appropriate length, including converting a 300-digit hexidecimal number into an equivalent 360- or 361-digit decimal number. Note that the return type of such a function would have to be String.
Got it!
You have to enter your alphabet as capital!
So if you put "AAAAA" and vise versa it's gonna work .
I don't know how to say thanks to you guys ,but I really apreciate your help and I hope I can help you in the future.
But I am not finish ,for sure I will come back with other question.
Especial thanks to AAAAAAAALLLLLLLLLLLLLLLLLL!
well , there is something I did for this in my progrma, I convert the Characters to upper case for comparison.
That way there wont be any problem, May be for the hex tool also you have to something like that.
I was right. After a bit of brainstorming, this was not difficult to code.
Code:
Option Explicit
Private Sub Command1_Click()
Dim strHex As String
Dim dblVar As Double
Dim n As Long
Dim msg As String
strHex = InputBox("Please enter a large hexidecimal value.", "HexToDec Test")
On Error Resume Next
dblVar = CDbl("&H" & Left(strHex, 15))
On Error GoTo 0
n = Len(strHex) - 15
If n > 0 Then dblVar = dblVar * 16 ^ n
msg = CStr(dblVar) & vbCrLf & HexToDec(strHex)
MsgBox msg, vbOKOnly, "HexToDec Test"
End Sub
Public Function HexToDec(strHex As String) As String
Const DigitRatio = 1.20411998265592 ' = Log(16) / Log(10)
Dim btDigit() As Byte
Dim lngMaxDigit As Long ' To keep track of actual number of decimal digits
Dim lngCarry As Long ' For carry-over during additions
Dim i As Long, j As Long
Dim btTemp As Byte
' Make array large enough to hold all digits
ReDim btDigit(Int(Len(strHex) * DigitRatio))
For i = 1 To Len(strHex) ' Loop through the string
' Check for valid hexidecimal digit and assign appropriate value to lngCarry
j = Asc(Mid(strHex, i, 1))
Select Case j
Case 48 To 57 ' 0 to 9
lngCarry = j - 48
Case 65 To 70 ' A to F
lngCarry = j - 55
Case 97 To 102 ' a to f
lngCarry = j - 87
Case Else ' not a valid hexidecimal digit
Exit For
End Select
' Multiply each decimal digit by 16 and add previous carry.
' This effectively multiplies the entire number by 16
' and adds the value of the current hexidecimal digit.
For j = 0 To lngMaxDigit
btDigit(j) = btDigit(j) * 16 + lngCarry
lngCarry = btDigit(j) \ 10
btDigit(j) = btDigit(j) Mod 10
Next j
' Continue until there is nothing to carry over, adding digits as necessary
Do Until lngCarry = 0
lngMaxDigit = lngMaxDigit + 1
btDigit(lngMaxDigit) = lngCarry Mod 10
lngCarry = lngCarry \ 10
Loop
Next i
' Remove any leading zeros
If lngMaxDigit < UBound(btDigit) Then
ReDim Preserve btDigit(lngMaxDigit)
End If
' Convert digit value to character code
For i = 0 To lngMaxDigit
btDigit(i) = btDigit(i) + 48
Next i
' Reverse the order of the digits
i = 0: j = lngMaxDigit
Do While i < j
btTemp = btDigit(i)
btDigit(i) = btDigit(j)
btDigit(j) = btTemp
i = i + 1
j = j - 1
Loop
'Convert to string and return
HexToDec = StrConv(btDigit, vbUnicode)
End Function
From hexidecimal to decimal, max number convertable approximately 33B2E3C9FD0803CE7FFFFFF
Code:
Function HexToDec(ByVal sHex As String) As Variant
Dim ba() As Byte, i&, bitval As Variant, b As Byte
bitval = 1
ba = sHex
For i = UBound(ba) - 1 To 0 Step -2
b = ba(i)
If b < 58 Then HexToDec = CDec(HexToDec + (b - 48) * bitval) Else HexToDec = CDec(HexToDec + (b - 55) * bitval)
bitval = CDec(bitval * 16)
Next i
End Function
well I coud convert
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
with the Conversion tools and my program both.
But to speak the thruth, the conversion progrma took some while to convert than my one. If you are considering for performance iseues, then I ll stick to my one.