Results 1 to 3 of 3

Thread: Problems with hex values stored in a textbox to integers

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    15

    Question Problems with hex values stored in a textbox to integers

    Hello everyone!

    I am reading back information on my serial port into a textbox and trying to convert it into an integer but hit some issues. The value comes in as a 6 separate characters, starting with "2A" if it's a good value, and then followed by 4 hex values. I basically am storing this into a character array, checking the first two letters to make sure they read "2A", then trying to convert the last 4 hex values into an integer.

    The issue I'm having is with the conversion from the char array to integer. Below is my current code:

    Code:
            Autozero As Integer = 0
    
            Dim temparray2() As Char = TextBox3.Text         'The hex values are stored in this textbox in string form
    
            If temparray2(0) = "2" And temparray2(1) = "A" Then
                    Autozero = Convert.ToInt16("&H" & temparray2(2) & temparray2(3) & temparray2(4) & temparray2(5))     
            End If
    The problem is the value is basically stored as:

    Code:
    "&H1FCC"
    rather than

    Code:
    &H1FCC
    If I do:

    Code:
    Autozero = Convert.ToString(&H1FCC)
    it works correctly. However, if I do:

    Code:
    Autozero = Convert.ToString("&H1FCC")
    which is essential what my program is doing above, I have a problem and it breaks here.

    I might actually have the wrong approach and maybe there's a better way to do this. If you have any suggestions, please let me know. Thanks!

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Problems with hex values stored in a textbox to integers

    vb Code:
    1. Dim ba As Byte() = Encoding.[Default].GetBytes("sample")
    2. Dim hexString As String= BitConverter.ToString(ba).Replace("-", String.Empty)
    As gotten from here:
    http://stackoverflow.com/questions/1...ing-in-c-sharp
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Problems with hex values stored in a textbox to integers

    You should NEVER use "&H" in text when working with hexadecimal numbers. That is specifically a prefix for hexadecimal literals in VB code. That's the only place it should be used. To convert a number to a hexadecimal string, I would recommend calling that number's ToString method. To convert from a hexadecimal string to an Integer, use Convert.ToInt32 or Integer.TryParse, e.g.
    Code:
    Dim n1 = 1000
    Dim n2 = &HA3F
    
    MessageBox.Show(n1.ToString("X"))
    MessageBox.Show(n2.ToString("x"))
    
    Dim h1 = "a1b2c3"
    Dim h2 = "DEF"
    Dim h3 = "fgh"
    
    Dim n3 = Convert.ToInt32(h1, 16)
    
    MessageBox.Show(n3.ToString())
    
    Dim n4 As Integer
    
    If Integer.TryParse(h2, NumberStyles.AllowHexSpecifier, Nothing, n4) Then
        MessageBox.Show(n4.ToString())
    Else
        MessageBox.Show("Not a number")
    End If
    
    Dim n5 As Integer
    
    If Integer.TryParse(h3, NumberStyles.AllowHexSpecifier, Nothing, n5) Then
        MessageBox.Show(n5.ToString())
    Else
        MessageBox.Show("Not a number")
    End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width