By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,711 Members | 1,372 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,711 IT Pros & Developers. It's quick & easy.

Converting Hexadecimal from a string variable to Decimal

P: n/a
I thought this would be easy but it is not.
I have tried:

Dim sLetter As String = Chr(&H4C) 'This works
Dim s As String = "&H4C"
sLetter = Chr(s) 'This causes an error.

My best guess right now is to do the following

Select Case sHex
Case "65"
s = "A"
Case "66"
s = "B"
etc.
End Select

Is there a better solution?

Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies

P: n/a
>I thought this would be easy but it is not.
I have tried:

Dim sLetter As String = Chr(&H4C) 'This works
Dim s As String = "&H4C"
sLetter = Chr(s) 'This causes an error.


Try

ChrW(CInt("&H4C"))

if that's what you want.


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2

P: n/a
"genojoe" <ge*****@discussions.microsoft.com> schrieb:
Dim sLetter As String = Chr(&H4C) 'This works
Dim s As String = "&H4C"
sLetter = Chr(s) 'This causes an error.


Use 'CInt' or 'Val' to parse the number represented in the string literal.
Then pass the resulting number to the 'Chr' function. I am curious why you
are storing a VB-style hexadecimal number literal in a string...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

P: n/a
Hope you see this. My experience with follow up posts is not very good.

Actually what I am trying to do is to read a binary file and parse it. I
could not find anything on the Web. After much searching and experimentation,
my code has evolved to:

Dim oFile As New FileInfo(FILE_NAME)
Dim iSize As Long = oFile.Length
Dim fs As New FileStream(FILE_NAME, FileMode.Open, FileAccess.Read)
Dim r As New BinaryReader(fs)
Dim arrBytes() As Byte = r.ReadBytes(CInt(iSize))
Dim i As Integer
Dim sbFile As New StringBuilder
Dim s0 As String = "0"
For i = 0 To arrBytes.Length - 1
sbFile = sbFile.Append(s0.Substring(0, 2 -
Hex(arrBytes(i)).Length) & Hex(arrBytes(i)).ToString)
Next
Dim str As String = sbFile.ToString

At this point, all I need to do is to convert 46 to "F" as shown in the
second char below. In the second example, I need to capture the numeric
value of the first 8 bytes.

Sample lines from the file can be:
20 46 49 4E 20 49 56 44 6F 20 4F 4E 4C 20 4F 55 - - - ( FIN IVDo ONL OU
or
30 A3 07 9B 48 63 C3 01 00 00 00 00 00 00 00 00 - - - (0...Hc..........)

I bet there is an easy way to do this parsing but I can't figure it out. My
approach seems to work but I need the conversion of 46 to "F", etc.

Your suggestion of Val() or CInt() does not work because neither is happy
with "4C".

PS. You have helped me numerous times, in most cases your response was
right on target. My current software project would not be where it is
without you. It seems like I almost know you. From the amount of responses
that you provide, it seems like you must be more than one person. Similar to
Mr. Goodwrench of GM.
Nov 21 '05 #4

P: n/a
kpg
genojoe <ge*****@discussions.microsoft.com> wrote in
news:C4**********************************@microsof t.com:
Sample lines from the file can be:
20 46 49 4E 20 49 56 44 6F 20 4F 4E 4C 20 4F 55 - - - ( FIN IVDo ONL
OU or
30 A3 07 9B 48 63 C3 01 00 00 00 00 00 00 00 00 - - -
(0...Hc..........)

I bet there is an easy way to do this parsing but I can't figure it
out. My approach seems to work but I need the conversion of 46 to
"F", etc.

Not elegant but it works. A loop would shorten the code.
? chr$(unhex("46"))
Public Function UnHex(hex As String) As Integer

'returns integer value of a two character hex string

Dim l As String
Dim h As String
Dim Tmp As Integer

l = UCase(Right$(hex, 1))
h = UCase(Left$(hex, 1))

If l >= "A" Then
Tmp = Asc(l) - 55
Else
Tmp = Val(l)
End If

If h >= "A" Then
Tmp = Tmp + ((Asc(h) - 55) * 16)
Else
Tmp = Tmp + Val(h) * 16
End If

UnHex = Tmp

End Function

kpg
Nov 21 '05 #5

P: n/a
"genojoe" <ge*****@discussions.microsoft.com> schrieb:
Your suggestion of Val() or CInt() does not work because neither is happy
with "4C".
That's because it expects a VB-style hex literal. Use the code below
instead:

\\\
Dim i As Integer = Convert.ToInt32("4C", 16)
///
PS. You have helped me numerous times, in most cases your response was
right on target. My current software project would not be where it is
without you. It seems like I almost know you. From the amount of
responses
that you provide, it seems like you must be more than one person. Similar
to
Mr. Goodwrench of GM.


Thank you :-)))!

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.