For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > April 2004 > Convert Hex to Dec









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 Convert Hex to Dec
Grace

2004-04-19, 11:30 am

I'm a new user of VB.
I have a number which is Hex. How can I convert it to a number which is Decimal?
Dim x as String
x = "FF"
Bob O`Bob

2004-04-27, 5:45 am

=?Utf-8?B?bXVzdGFmYQ==?= wrote:
>
> Hi I want to convert 10 digit decimal number to heaxdecimal number i tried
> with hex function but it take only upto 9 digit dec no . for 10 digit no it
> throw an error "overflow". i am using vb as my programing language.
> If anybody knows how to do it please let me know.



I thought it might be fun to try ... must be the late hour.

Anyway the following might help.
It appears to be good for at least 13 decimal digits.

Just be warned this is "up too late" code. It seems to me to be
working great, but then again my test methods might be influenced
by the late hour too.

As always I welcome any corrections, suggestions, banter, whatever...


Option Explicit
Const invert = "fedcba9876543210"

Public Function BigHex(ByVal d As Double) As String
Dim b As String, p As Double
If d < 0 Then
b = NotHex(BigHex(-d - 1))
b = String(4 - (Len(b) Mod 4), "f") & b
Else
d = Fix(d) 'throw away any fraction
Do
'substitute because mod() is limited to the range of a long
p = d - 16 * Fix(d / 16) 'p = d Mod 16
d = (d - p) / 16
Debug.Assert d = Fix(d)
b = Hex(p) & b
Loop While d
End If
BigHex = b
End Function

Private Function NotHex(s As String) As String
'*assumes* input consists of only hex
Dim i As Long, x As Long
NotHex = String(Len(s), " ")
For i = 1 To Len(s)
x = 1 + CLng("&h" & Mid$(s, i, 1))
Mid$(NotHex, i, 1) = Mid$(invert, x, 1)
Next
End Function


Oh yeah, and I threw in negative numbers too, just for fun, and
assumed (for no particular reason) that they should be padded to
multiples of 16 bits.


Bob
--
Bob O`Bob

2004-04-27, 3:32 pm

Bob O`Bob wrote:

> Just be warned this is "up too late" code. It seems to me to be
> working great, but then again my test methods might be influenced
> by the late hour too.


Ha!
I found a serious crash bug this morning.

Quiz: Anyone else notice?

Hint: "Out of stack space"


Bob
--
Rick Rothstein

2004-04-27, 3:36 pm

"mustafa" <mustafakh@yahoo.com> wrote in message
news:CB7FD96D-D98E-4FB5-8A6E-4CC057166EB2@microsoft.com...
> Hi I want to convert 10 digit decimal number to heaxdecimal number i

tried with hex function but it take only upto 9 digit dec no . for 10
digit no it throw an error "overflow". i am using vb as my programing
language.
> If anybody knows how to do it please let me know.
>


Okay, I'm a bit (not pun intended) ... your Subject says you
want to convert Hex to Decimal, but your message seems to indicate you
want to go the other way. Either way, here is a past posting of mine
which should cover whatever you need.

Rick - MVP

If you want to work with unsigned Integers (actually, Longs -- 32bits),
then give this function a try (see the end of this message for if you
need to handle even larger numbers).

Function UnsignedDec2Hex(ByVal Value As Variant) As String
Dim TwoToThe32 As Variant
TwoToThe32 = CDec("2") ^ 32
If CDec(Value) < 0 Or _
Abs(CDec(Value)) >= TwoToThe32 Then
UnsignedDec2Hex = -1
Else
If CDec(Value) >= TwoToThe32 / 2 Then
Value = CDec(Value) - TwoToThe32
End If
UnsignedDec2Hex = Hex$(CDec(Value))
End If
End Function

It will return -1 if the Value passed into the function is less than 0
or greater then 4294967295.

And I thought you might be interested in this (sort of) companion
function... it should correctly give integer results for numbers up to
96 bits (far more than would ever be needed).

Function Hex2UnsignedDec(HexString As String) As Variant
Dim X As Integer
Dim BinStr As String
If Len(HexString) <= 12 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
For X = 0 To Len(BinStr) - 1
Hex2UnsignedDec = Hex2UnsignedDec + Val(Mid(BinStr, _
Len(BinStr) - X, 1)) * 2 ^ X
Next
Else
' Number is too big, handle error here
End If
End Function

However, if you need to handle even larger numbers, then how about
numbers up to 28 digits (if the number is greater than 4294967295, you
should pass it into the function as a String)? 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> ).

Function Dec2Hex(ByVal DecimalIn As Variant) As Variant
If Len(DecimalIn) < 29 Then
Dec2Hex = Bin2Hex(Dec2Bin(DecimalIn))
Else
Dec2Hex = -1
End If
End Function

'Decimal to Binary
'=====================
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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com