Displaying decimal value, want to display Hex value

ejleiss

Active member
Joined
Oct 1, 2012
Messages
37
Programming Experience
1-3
Could anybody help me convert the values displayed in my rich text box from decimal format to hexidecimal?
My code is as follows:

Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports
Public Class frmMain
    Dim myPort As Array  'COM Ports detected on the system will be stored here
    Delegate Sub SetTextCallback(ByVal [text] As Byte) 'Added to prevent threading errors during receiveing of data

    ' Dim rxBuff As String 'Buffer for receievd data

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.
        myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available
        cmbBaud.Items.Add(9600)     'Populate the cmbBaud Combo box to common baud rates used
        cmbBaud.Items.Add(19200)
        cmbBaud.Items.Add(38400)
        cmbBaud.Items.Add(57600)
        cmbBaud.Items.Add(115200)

        For i = 0 To UBound(myPort)
            cmbPort.Items.Add(myPort(i))
        Next
        cmbPort.Text = cmbPort.Items.Item(0)    'Set cmbPort text to the first COM port detected
        cmbBaud.Text = cmbBaud.Items.Item(0)    'Set cmbBaud text to the first Baud rate on the list

        btnDisconnect.Enabled = False           'Initially Disconnect Button is Disabled

    End Sub

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        SerialPort1.PortName = cmbPort.Text         'Set SerialPort1 to the selected COM port at startup
        SerialPort1.BaudRate = cmbBaud.Text         'Set Baud rate to the selected value on

        'Other Serial Port Property
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.DataBits = 8            'Open our serial port
        SerialPort1.Open()

        btnConnect.Enabled = False          'Disable Connect button
        btnDisconnect.Enabled = True        'and Enable Disconnect button


    End Sub

    Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
        SerialPort1.Close()             'Close our Serial Port

        btnConnect.Enabled = True
        btnDisconnect.Enabled = False
    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        SerialPort1.Write(txtTransmit.Text & vbCr) 'The text contained in the txtText will be sent to the serial port as ascii
        'plus the carriage return (Enter Key) the carriage return can be ommitted if the other end does not need it
    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
   

        ReceivedText(SerialPort1.ReadByte()) 'Automatically called every time a data is received at the serialPort

        ' ReceivedText(str)


    End Sub
    Private Sub ReceivedText(ByVal [text] As Byte)
        'compares the ID of the creating Thread to the ID of the calling Thread
        '  Dim hex As Integer
        '  hex = text.ToString("x")

        ' From Dec to Hex:
        'Dim text As String
        'text = "&H" & Hex$(4095)

        If Me.rtbReceived.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
            Me.rtbReceived.Text &= [text]
        End If
    End Sub

    Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
        If SerialPort1.IsOpen = False Then
            SerialPort1.PortName = cmbPort.Text         'pop a message box to user if he is changing ports
        Else                                                                               'without disconnecting first.
            MsgBox("Valid only if port is Closed", vbCritical)
        End If
    End Sub

    Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged
        If SerialPort1.IsOpen = False Then
            SerialPort1.BaudRate = cmbBaud.Text         'pop a message box to user if he is changing baud rate
        Else                                                                                'without disconnecting first.
            MsgBox("Valid only if port is Closed", vbCritical)
        End If
    End Sub

  
End Class
 
You've already got it... almost:
VB.NET:
        '  Dim hex As Integer
        '  hex = text.ToString("x")
Does it make sense to call ToString and then assign the result to an Integer variable? ToString returns a String and the Text of a RichTextBox is a String so it should be fairly obvious what to do.
 
I'm new to this forum, but been using .NET since 2005. jmcilhinney, has a very good point about assigning a string to an int. Turn OPTION STRICT ON... it helps prevents a lot of problems. I just started working at a company that don't use STRICT ON and it's driving me crazy because they are wondering why the have Null Exceptions for "no reason" :)
 
I'm new to this forum, but been using .NET since 2005. jmcilhinney, has a very good point about assigning a string to an int. Turn OPTION STRICT ON... it helps prevents a lot of problems. I just started working at a company that don't use STRICT ON and it's driving me crazy because they are wondering why the have Null Exceptions for "no reason" :)

I feel your pain. My office didn't used to have Option Strict On either. They were originally VB6 developers so they didn't see the issue. We use C# mostly these days so it's a moot point but I advise everyone to turn it On in the IDE settings so that every future project will be that way from the start.
 
The company I just started at said they went straight from VB6 to VB2005... They (small company, 5 programmers) admit that they didn't know .net. And they are still using VS2005 :( No big deal there, but I copied one on the projects to my local drive and turned strict on and the last error I had said "there were too many errors to report"!! I took a class in .NET many years back because the company I worked for then sent us for continued education. First thing the instructor said about VB.net was turn Option Strict on. I've lived by that after hearing horror stories.

Sorry to get off thread about the question posted.
 
You've already got it... almost:
VB.NET:
        '  Dim hex As Integer
        '  hex = text.ToString("x")
Does it make sense to call ToString and then assign the result to an Integer variable? ToString returns a String and the Text of a RichTextBox is a String so it should be fairly obvious what to do.

Thank you, that was an obvious fix however now with the following code I get an error saying 'Runtime errors may occur when converting 'String' to 'System.IFormatProvider'
 Private Sub ReceivedText(ByVal [text] As String)
        'compares the ID of the creating Thread to the ID of the calling Thread
        Dim hex As String
        hex = text.ToString("x")

        If Me.rtbReceived.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
            Me.rtbReceived.Text &= [hex]
        End If
    End Sub
 
Back
Top