Help needed:Converting HEX to DECIMAL

Thread Starter

weiix05

Joined Apr 2, 2009
15
I am using visual basic 2005 for my project. I am done with the part reading th values from the board. The values come out in HEX so i need to convert it to decimal,can anyone help me with the code for visual basic 2005 to convert HEX to decimal.

Thanks.:)
 

Thread Starter

weiix05

Joined Apr 2, 2009
15
PublicClass Form1
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call Read()
EndSub
PrivateSub Read()
Dim value AsInteger
Dim hex_val AsString
If SerialPort1.IsOpen Then
SerialPort1.Close()
EndIf
SerialPort1.PortName = "COM1"
SerialPort1.BaudRate = 9600
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.DataBits = 8
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.RtsEnable = True
SerialPort1.Open()
SerialPort1.WriteLine("U2" + vbCr)
TextBox1.Text = vbNewLine & "....Work in progress...."
TextBox1.Refresh()
System.Threading.Thread.Sleep(150)
TextBox1.Text = ""
TextBox1.Text = SerialPort1.ReadExisting
SerialPort1.Close()
EndSub
EndClass




this is currently my code, i need to change the hex values to decimal in the textbox.
 

t_n_k

Joined Mar 6, 2009
5,455
You might try ToInt32() function...

Dim Hex_val as string = "ABC111"
Dim
i As Integer = Convert.ToInt32(Hex_val, 16)

should give i=11256081

This works for VB2008
 

Thread Starter

weiix05

Joined Apr 2, 2009
15
Sorry i am new to Vb2005, i dont understand about the function part. I am so sorry, can tell me where to put it?
 

t_n_k

Joined Mar 6, 2009
5,455
From what I can deduce from your code you have the following, so far:-

A button - "Button1"
A text box - "TextBox1"
A serial port interface - "SerialPort1"

You click the button to open the port through a private subroutine "read()" and send an ASCII string "U2" with CR. Whenever you send the data you write the phrase "....Work in progress...." to the text box. You sleep 150ms to enable the serial port to load in the received data.

Two variables - "value" [an integer] and "hex_val" [a string] are declared but not used.

Your code appears incomplete - you read data from the port which will probably come as a hexadecimal string. When data is read it is copied into the text box - as the hex string not as the integer value.

If I understand you correctly, it seems you actually want to convert the received hex string to a decimal (integer) equivalent first then write to the text box.

So you need some extra code to :-

1. copy the hexadecimal string to string variable "hex_val"
2. convert the hexadecimal string to an integer "value"
3. place the integer value in the text box.

Therefore I would modify / add code as follows ...

after the line

TextBox1.Text = ""

delete the line - TextBox1.Text = SerialPort1.ReadExisting

then insert the following lines..

hex_val = SerialPort1.ReadExisting
value=Convert.ToInt32(hex_val, 16)
TextBox1.text=str(value)

Everything else stays the same.

That might get you closer to a solution if the port read works OK - as you indicate it does.
 

Thread Starter

weiix05

Joined Apr 2, 2009
15
Thanks tnk, but i am still facing some probs.

PublicClass Form1
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call Read()
EndSub
PrivateSub Read()
Dim value AsInteger
Dim hex_val AsString
If SerialPort1.IsOpen Then
SerialPort1.Close()
EndIf
SerialPort1.PortName = "COM1"
SerialPort1.BaudRate = 9600
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.DataBits = 8
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.RtsEnable = True
SerialPort1.Open()
SerialPort1.WriteLine("U2" + vbCr)
TextBox1.Text = vbNewLine & "....Work in progress...."
TextBox1.Refresh()
System.Threading.Thread.Sleep(150)
TextBox1.Text = ""
hex_val = SerialPort1.ReadExisting
value = Convert.ToInt32(hex_val, 16)
TextBox1.Text = Str(value)

SerialPort1.Close()
EndSub

PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
EndSub
EndClass





this is my edited code according to your help,
but there is still an error when i debug it.
value = Convert.ToInt32(hex_val, 16) --->the error is could not find any recognizable digits.
 

t_n_k

Joined Mar 6, 2009
5,455
Not really sure - Looks like there may be no valid ASCII printable data received at the serial port from the connected device or non-printable leading bytes such as control characters. I'm guessing you must be receiving something - since you see hex characters being received. Do the hex characters you see look as if they are printable ASCII - check any ASCII / Hex table.

http://www.serial-port-monitor.com/index.html

Download the above free serial port monitor and run this before you run your application - it can provide a watch of the port transmissions which might prove useful.

You may have to extract the ASCII printable characters from the incoming data stream before conversion to ASCII format using the Convert.ToInt32(hex_val, 16) instruction. What's the data format you expect to see from the connected device - i.e. "the board"?

It might also be worth adding a sub which is waiting for data to actually arrive at the port - rather than just using a 150ms sleep.

Like this ...

Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
[Insert Code for ... whatever you want to do with the data, including stripping out non-printable characters]
End Sub
 

Thread Starter

weiix05

Joined Apr 2, 2009
15
Thanks tnk. Now i was told to extract the last 3 digits of the hexadecimal i received den covert it one by one to decimal. Can someone pls help me?

Thanks.
 

t_n_k

Joined Mar 6, 2009
5,455
Now i was told to extract the last 3 digits of the hexadecimal i received den covert it one by one to decimal. Can someone pls help me?

Thanks.
What is the incoming hexadecimal sequence - is it a fixed length and how many bytes are received? Is there a fixed byte that always precedes the three bytes of interest?

You need to ignore any bytes preceding the three important ones but you need to know which ones to ignore.
 

Thread Starter

weiix05

Joined Apr 2, 2009
15
Etc of the hexadecimal is U207F. But i dont need th first two values. I just need to convert the last three values one by one to decimal. I did manage to covert the numeric number but once it come out in alphabet i cant convert.




Private Sub Convert()
Dim LastDigit As String
Dim intLastDigit, intSecDigit, intFirstDigit As Integer


LastDigit = TextBox1.Text.Substring(4, 1)
intLastDigit = LastDigit * 16 ^ 0

Dim SecDigit As String
SecDigit = TextBox1.Text.Substring(3, 1)
intSecDigit = SecDigit * 16 ^ 1
Dim FirstDigit As String
FirstDigit = TextBox1.Text.Substring(2, 1)
intFirstDigit = FirstDigit * 16 ^ 2
TextBox2.Text = intLastDigit + intSecDigit + intFirstDigit



End Sub
End
Class


this is my code to convert only numeric number.
 

t_n_k

Joined Mar 6, 2009
5,455
Which you could do as ..

PrivateSub Convert()

Dim my_str as String = Textbox1.Text.Substring(2,3) ; Form the 3 digit string.

TextBox2.text=Convert.ToInt16(my_str,16) ; Convert it to integer value & display it.

EndSub
 

t_n_k

Joined Mar 6, 2009
5,455
Or do it this way ....probably much easier to appreciate. Hopefully it works!

PrivateSub Convert()

Dim my_str as String = Textbox1.Text.Substring(2,3) ; Form the 3 digit string.

my_str="&H"+my_str ; Now it looks like HEX format

textbox2.text=Val(my_str) ; Val knows how to handle hex formats.

EndSub

You can just do it in one line ... it's a matter of convenience.

textbox2.text=Val("&H"+my_str) ; Val knows how to handle hex formats.

:)
 

t_n_k

Joined Mar 6, 2009
5,455
Strictly speaking Val should be made string type before putting in the textbox .. 'though VB2008 doesn't object.

i.e.

Textbox2.Text=Str(Val(my_str)) ; Make the value a string type.

Don't use semi-colons as comment delimiter in the code BTW!

That's all done for me .... good luck! Sorry about the interminable posts! Nice long thread!
 

t_n_k

Joined Mar 6, 2009
5,455
A small P.S.

I forgot about the CInt() function as well

Textbox2.Text=Str(CInt(my_str))

also gives the same result as Val()

where my_str="&HXXX" for your case & X can be any of hex values 0,1,2,3,....,A,B,C,D,E,F

So you have several options .....something should work for you.
 
Top