Home > Archive > Visual Basic Syntax > April 2005 > converting Decimal to Hex
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 |
converting Decimal to Hex
|
|
| bigbob 2005-04-25, 4:01 pm |
| Anybody have a snippet to convert very large (trillions) decimal numbers to
hex?
| |
| Rick Rothstein 2005-04-25, 8:59 pm |
| > Anybody have a snippet to convert very large (trillions)
> decimal numbers to hex?
Hoping by "trillions", you mean the size of the number and not the
number of digits making up the number.<g>
From a previous post of mine...
As others have told you, an unlimited large number is not possible
directly; however, how about numbers up to 28 digits? It's sort of a
round-about way, but the following will handle integer values up to
9999999999999999999999999999 (which is 204FCE5E3E2502610FFFFFFF if any
one is interested). You use Dec2Hex which internally uses the Dec2Bin
and the Bin2Hex functions (yeah, I know, I could have combined all of
this into a single routine, but I had these readily lying around <g> ).
Rick - MVP
Function Dec2Hex(ByVal DecimalIn As Variant) As Variant
If Len(DecimalIn) < 29 Then
Dec2Hex = Bin2Hex(Dec2Bin(DecimalIn))
Else
Dec2Hex = -1
End If
End Function
Function Dec2Bin(ByVal DecimalIn As Variant, _
Optional NumberOfBits As Variant) As String
Dec2Bin = ""
DecimalIn = Int(CDec(DecimalIn))
Do While DecimalIn <> 0
Dec2Bin = Trim$(Str$(DecimalIn - _
2 * Int(DecimalIn / 2))) & Dec2Bin
DecimalIn = Int(DecimalIn / 2)
Loop
If Not IsMissing(NumberOfBits) Then
If Len(Dec2Bin) > NumberOfBits Then
Dec2Bin = "Error - Number exceeds specified bit size"
Else
Dec2Bin = Right$(String$(NumberOfBits, _
"0") & Dec2Bin, NumberOfBits)
End If
End If
End Function
'Binary to Hexadecimal
'=====================
Function Bin2Hex(ByVal BinaryString As String) As String
Dim X As Integer
Const BinValues = "*0000*0001*0010*0011" & _
"*0100*0101*0110*0111" & _
"*1000*1001*1010*1011" & _
"*1100*1101*1110*1111*"
Const HexValues = "0123456789ABCDEF"
If BinaryString Like "*[!01]*" Then
Bin2Hex = "Error - Argument not a binary string"
Else
BinaryString = String$((4 - Len(BinaryString) _
Mod 4) Mod 4, "0") & BinaryString
For X = 1 To Len(BinaryString) - 3 Step 4
Bin2Hex = Bin2Hex & Mid$(HexValues, _
(4 + InStr(BinValues, "*" & _
Mid$(BinaryString, X, 4) & "*")) \ 5, 1)
Next
End If
End Function
| |
| Jeff Johnson [MVP: VB] 2005-04-25, 8:59 pm |
|
"Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message
news:%23mucXTcSFHA.1972@TK2MSFTNGP10.phx.gbl...
>
> Hoping by "trillions", you mean the size of the number and not the
> number of digits making up the number.<g>
....or the amount of numbers to be converted.
| |
| bigbob 2005-04-26, 4:03 pm |
| Thanks Rick. Works fine..
"Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message
news:%23mucXTcSFHA.1972@TK2MSFTNGP10.phx.gbl...
>
> Hoping by "trillions", you mean the size of the number and not the
> number of digits making up the number.<g>
>
> From a previous post of mine...
>
> As others have told you, an unlimited large number is not possible
> directly; however, how about numbers up to 28 digits? It's sort of a
> round-about way, but the following will handle integer values up to
> 9999999999999999999999999999 (which is 204FCE5E3E2502610FFFFFFF if any
> one is interested). You use Dec2Hex which internally uses the Dec2Bin
> and the Bin2Hex functions (yeah, I know, I could have combined all of
> this into a single routine, but I had these readily lying around <g> ).
>
> Rick - MVP
>
> Function Dec2Hex(ByVal DecimalIn As Variant) As Variant
> If Len(DecimalIn) < 29 Then
> Dec2Hex = Bin2Hex(Dec2Bin(DecimalIn))
> Else
> Dec2Hex = -1
> End If
> End Function
>
> Function Dec2Bin(ByVal DecimalIn As Variant, _
> Optional NumberOfBits As Variant) As String
> Dec2Bin = ""
> DecimalIn = Int(CDec(DecimalIn))
> Do While DecimalIn <> 0
> Dec2Bin = Trim$(Str$(DecimalIn - _
> 2 * Int(DecimalIn / 2))) & Dec2Bin
> DecimalIn = Int(DecimalIn / 2)
> Loop
> If Not IsMissing(NumberOfBits) Then
> If Len(Dec2Bin) > NumberOfBits Then
> Dec2Bin = "Error - Number exceeds specified bit size"
> Else
> Dec2Bin = Right$(String$(NumberOfBits, _
> "0") & Dec2Bin, NumberOfBits)
> End If
> End If
> End Function
>
> 'Binary to Hexadecimal
> '=====================
> Function Bin2Hex(ByVal BinaryString As String) As String
> Dim X As Integer
> Const BinValues = "*0000*0001*0010*0011" & _
> "*0100*0101*0110*0111" & _
> "*1000*1001*1010*1011" & _
> "*1100*1101*1110*1111*"
> Const HexValues = "0123456789ABCDEF"
> If BinaryString Like "*[!01]*" Then
> Bin2Hex = "Error - Argument not a binary string"
> Else
> BinaryString = String$((4 - Len(BinaryString) _
> Mod 4) Mod 4, "0") & BinaryString
> For X = 1 To Len(BinaryString) - 3 Step 4
> Bin2Hex = Bin2Hex & Mid$(HexValues, _
> (4 + InStr(BinValues, "*" & _
> Mid$(BinaryString, X, 4) & "*")) \ 5, 1)
> Next
> End If
> End Function
>
|
|
|
|
|