Converting Hexadecimal from a string variable to Decimal

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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
 
genojoe said:
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...
 
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.
 
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
 
genojoe said:
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 :-)))!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top