Convert a string to Hex

Gopher2011

Well-known member
Joined
Mar 3, 2011
Messages
111
Location
South England
Programming Experience
10+
Hi, I have 1 string of 12 numbers separated by a ',' "11.11,22.22,33.33" etc coming in my serial port. One number has so far been 5339, but now its changed to 533d, which causes problems, for reasons I am still trying to work out.

The top numbers are all real numbers ie 0.000 or 00.00 or 000.0, whereas the bottom 3 numbers are Hex FFFFF but just happen to show decimal numbers by 'good luck'.

When the Hex numbers show 'decimals' they convert/whatever ok, but showing abcdef hex they trip it up.. I know its the Integer part of Word0. However I am out of ideas.

Any advice?


VB.NET:
    Public Number_01 As Integer
    .....
    Public Number_09 As Integer
    Public Word0 As Integer
    Public Word1 As Integer
    Public Word2 As Integer

    Private Sub Sort()

        Dim i As Integer
        Dim SortedRxText() As String
        SortedRxText = txt_Latest_ComsIn.Text.Split(",")

        For i = 0 To UBound(SortedRxText)
            Debug.WriteLine(i & " " & SortedRxText(i))
            If NxtExptData4 = "l" Then
                Select Case i
                    Case i : Anal_RAW(i) = Convert.ToDouble(SortedRxText(i))
                End Select

                'Wait till all the data is read before copying it.
                If i = 13 Then
                    Number_01 = CDbl(Analg_RAW(1))
                    Number_02 = CDbl(Analg_RAW(2))
                    Number_03 = CDbl(Analg_RAW(3))
                    Number_04 = CDbl(Analg_RAW(4))
                    Number_05 = CDbl(Analg_RAW(5))
                    Number_06 = CDbl(Analg_RAW(6))
                    Number_07 = CDbl(Analg_RAW(7))
                    Number_08 = CDbl(Analg_RAW(8))
                    Number_09 = CDbl(Analg_RAW(9))
                    Word0 = "&H" & Analg_RAW(10)
                    Word1 = "&H" & Analg_RAW(11)
                    Word2 = "&H" & Analg_RAW(12)
                End If

            End If
        Next i

In the debug it looks like this:-

VB.NET:
0 0000
1 -2.8
2 1010
3 0.0
4 0166
5 21.3
6 0153
7 373
8 1011
9 5698
10 0000
11 5339

However value <11> has changed to 533d And debug throws an exception now..

VB.NET:
0 0000
1 -2.8
2 1010
...
10 0000
11 533d
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
 
I would have expected them all to be Decimal OR Hex, not a mixture of the two. Are you sure they aren't all Hex?
 
Ah, sorry for the delay InertiaM - I totally missed this.

They are indeed a mix.
the ascii string coming in the com port contains a number of numbers '1.111', '2.222'... etc, then at the 3 'flag' numbers expressed as 3 x 8 bit words.

ie.

11010111
10100110
01101101

These are sent as ascii.
I need to turn the strings "006c,0000,4012" into an integer for storage in my array.
Do you have any ideas?

I found

VB.NET:
Dim ic As Integer = 255
Dim sc As String
sc = ic.ToString("X")


But cant get it to work.
 
I'm slightly confused, as the code you posted is trying to convert an integer into a hexadecimal value - but your posts suggest you want hex to decimal.

The easiest way to do hex to decimal is to append 0x at the front, and then use Convert.ToInt32 (or whatever you want), with 16 as the FromBase parameter.

VB.NET:
        Dim sc As String = "006c"
        Dim ic As Integer = 0

        Try
            ic = Convert.ToInt32(String.Concat("0x", sc), 16)
            MessageBox.Show(ic.ToString)
        Catch ex As Exception
            'not a valid hex number
        End Try
 
Hi,
This works with the first 2 of my data packets in the string-

'006c,0000,4012'.

However the third string casues an exception. Do you have any ideas InertiaM?

I is a for loop of the long string of text separated by a ',' broken up and stored in an array RxText(i).
1-16 are integers and work just fine. 17-19 are hex(stored as strings).

For some reason the

VB.NET:
Dim RTU_Anlgs(20) As Integer 
Dim RxText(20) As String
...

Select Case i
  Case 1 To 16 : RTU_Anlgs(i) = Convert.ToDouble(RxText(i))
  Case 17 To 19
    Debug.WriteLine(i)
    ic = Convert.ToInt32(String.Concat("0x", RxText(i)), 16)
    Debug.WriteLine("____" & ic.ToString & "____")
    RTU_Anlgs(i) = ic
  End Select



I will put the debug here -

VB.NET:
17
____108____
18
____0____
19
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
 
Two guesses.

First is that there is a space after the 4012. Second is that there is an unprintable character. Check the length of the string being converted.

Hint:- When trying to debug string conversions that are generating errors, view the string value AND the length of the string for attempted conversion *before* converting :)

VB.NET:
    Debug.WriteLine(i)
    Debug.WriteLine(String.format ("[{0}], length {1}", RxText(i), RxText(i).Length)
    ic = Convert.ToInt32(String.Concat("0x", RxText(i)), 16)
    Debug.WriteLine("____" & ic.ToString & "____")
 
Hey nice guess - I like your logic - here is your debug - it looks like their was a vbCR or "\r" in the string.

17
[006c], length 4
____108____
18
[0000], length 4
____0____
19
[4012
], length 5

I will investigate...
 
Fixed, thank you once more InertiaM . I think I was focusing on the conversion without thinking of the simple base level approach.

17
[006c], length 4
____108____
18
[0000], length 4
____0____
19
[4012], length 4
____16402____
 
Back
Top