Home > Archive > Visual Basic Syntax > August 2005 > Large number to HEX conversion
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Large number to HEX conversion
|
|
| Guy Cohen 2005-08-17, 9:21 am |
| Hi all.
How can I convert this number: 2227808324
To hex?
(84C9A844)
Using Hex$ in VB6 returns an error
TIA
Guy
| |
|
| Guy Cohen wrote:
> Hi all.
>
> How can I convert this number: 2227808324
> To hex?
> (84C9A844)
>
> Using Hex$ in VB6 returns an error
>
> TIA
> Guy
>
>
Your number is larger than a signed 32bit integer.
VB can't convert that for you. You'll have to split it.
Sinna
| |
| Trimbitas Sorin 2005-08-17, 9:21 am |
| Hello
Write yourself a function to convert base 10 numbers to base 16 (hexa).
The algorithm is very simple :
->while the number is <> 0 get the rest of its division with 16 and
translate it to HEX (ex: 1 will become 1, 2 will become 2, ... 10 will
become A, 11 will become B and so on).
-- pseudocode
str=""
while n<>0
rest=n/16
str=str & translate(rest)
n=n/16
end while
function translate(number) as string
case 0 : return "0"
case 1 : return "1"
...
case 9 : return "9"
case 10 : return "A"
....
case 15 : return "F"
end function
--
HTH
"Guy Cohen" <noemail@please.com> wrote in message
news:%23Ax1JUxoFHA.2484@TK2MSFTNGP15.phx.gbl...
> Hi all.
>
> How can I convert this number: 2227808324
> To hex?
> (84C9A844)
>
> Using Hex$ in VB6 returns an error
>
> TIA
> Guy
>
| |
| Rick Rothstein [MVP - Visual Basic] 2005-08-17, 9:21 am |
| > How can I convert this number: 2227808324
> To hex?
> (84C9A844)
Consider the following previous post of mine.
Rick
Try these two functions (feed very large decimal numbers into the
Dec2Hex function as a String value so VB doesn't first convert it to
E-Notation and truncated it down to 15 significant digits)...
Function Dec2Hex(ByVal DecimalIn As Variant) As String
Dim X As Integer
Dim BinaryString As String
Const BinValues = "*0000*0001*0010*0011" & _
"*0100*0101*0110*0111" & _
"*1000*1001*1010*1011" & _
"*1100*1101*1110*1111*"
Const HexValues = "0123456789ABCDEF"
Const MaxNumOfBits As Long = 96
BinaryString = ""
DecimalIn = Int(CDec(DecimalIn))
Do While DecimalIn <> 0
BinaryString = Trim$(Str$(DecimalIn - 2 * _
Int(DecimalIn / 2))) & BinaryString
DecimalIn = Int(DecimalIn / 2)
Loop
BinaryString = String$((4 - Len(BinaryString) _
Mod 4) Mod 4, "0") & BinaryString
For X = 1 To Len(BinaryString) - 3 Step 4
Dec2Hex = Dec2Hex & Mid$(HexValues, _
(4 + InStr(BinValues, "*" & _
Mid$(BinaryString, X, 4) & "*")) \ 5, 1)
Next
End Function
Function Hex2Dec(ByVal HexString As String) As Variant
Dim X As Integer
Dim BinStr As String
Const TwoToThe49thPower As String = "562949953421312"
If Left$(HexString, 2) Like "&[hH]" Then
HexString = Mid$(HexString, 3)
End If
If Len(HexString) <= 23 Then
Const BinValues = "0000000100100011" & _
"0100010101100111" & _
"1000100110101011" & _
"1100110111101111"
For X = 1 To Len(HexString)
BinStr = BinStr & Mid$(BinValues, _
4 * Val("&h" & Mid$(HexString, X, 1)) + 1, 4)
Next
Hex2Dec = CDec(0)
For X = 0 To Len(BinStr) - 1
If X < 50 Then
Hex2Dec = Hex2Dec + Val(Mid(BinStr, _
Len(BinStr) - X, 1)) * 2 ^ X
Else
Hex2Dec = Hex2Dec + CDec(TwoToThe49thPower) * _
Val(Mid(BinStr, Len(BinStr) - X, 1)) * 2 ^ (X - 49)
End If
Next
Else
' Number is too big, handle error here
End If
End Function
|
|
|
|
|