locked
convert RRS feed

  • Question

  •  

    Hi,

     

    I have another question...

    my program is , i have to receive data from external hardware which is sensor. That sensor will send me a long string and with some coding, i managed to extract those related data from the comp port.from there,  i use a text box to display the result and it display properly..

    But now i want to convert the data that being displayed on the textbox to decimal form, but my proble is bcoz the data that i have been receive got space between them,so i can just convert it ..what should i do?

    there is something like this:

     

    data extracted: CC FF

    i want to convert it to decimal,but there are spaces between it,how should i do?

    can i concate the string?

     

    thanks in advance..

    Friday, September 7, 2007 9:09 AM

Answers

  • Here is an example:

     

    Code Snippet

    'STARTING STRING

    Dim MyString As String = "CC FF"

     

    'SPLIT UP THE VALUES AT THE SPACES

    Dim MyValues() As String = MyString.Split(" "c)

     

    'LOOP ARRAY, CONVERTING EACH VALUE TO DECIMAL VALUES (YOU NEED TO ADD &H SO VB KNOWS ITS A HEX VALUE)

    For Each S As String In MyValues

    MessageBox.Show(CInt("&H" & S).ToString)

    Next

     

     

     

    Friday, September 7, 2007 2:15 PM
    Moderator

All replies

  • Here is an example:

     

    Code Snippet

    'STARTING STRING

    Dim MyString As String = "CC FF"

     

    'SPLIT UP THE VALUES AT THE SPACES

    Dim MyValues() As String = MyString.Split(" "c)

     

    'LOOP ARRAY, CONVERTING EACH VALUE TO DECIMAL VALUES (YOU NEED TO ADD &H SO VB KNOWS ITS A HEX VALUE)

    For Each S As String In MyValues

    MessageBox.Show(CInt("&H" & S).ToString)

    Next

     

     

     

    Friday, September 7, 2007 2:15 PM
    Moderator
  • If you use the Val function, it will convert your Hex data to Decimal, ignoring any spaces, so you don't have to worry about that.  Here is a sample:

     

    Code Snippet

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim hexnum As String, decnum As Double

    hexnum = TextBox1.Text

    decnum = Val("&H" & hexnum & "&")

    TextBox2.Text = decnum.ToString

    End Sub

     

     

     

    Friday, September 7, 2007 4:53 PM
  • 1st, thank for the reply..really appreciate..

     

    Is there have any method that can ignore the spacing between the string?

    for example, for this long string:

     

    " CC FF 0A 00 00 00 00 02 A2 FF CC"

     

    can i do something to make it become something like this:

     

    "CCFF0A0000000002A2FFCC"

     

    Thanks you very much..

     

    Monday, September 10, 2007 2:36 AM
  •  

    I have run both of the program...

     

    for the 1st one, how if the MyString is not a fixed number?

    In my case, the MyString is feed from the data that being extracted from Com port and the data from com port is changeable and it is not a fixed number...

    for example at the 1st time the com port might get a long data like :

     

    CC FF 12 20 A0 00 00 00 00 01 0E A0 0F A1 FF CC

     

    I want to convert the Hex(in blue ) color to decimal and stored it at a variable and Hex(red color) in another variable...bcoz i want to do a if..else statement...

     

    while for the second program, i have copied and run it.yes, that's right that the computer will automatically ignore the spacing between themself...but if what I said earlier on, if i want to convert like the A0 0F at top (in blue color), is that i have/ must extract that specified hex number out 1st b4 i can convert it?

     

    thanks for the precious advise...

     

    Monday, September 10, 2007 3:43 AM
  • Did you try running the code I posted?  It will remove all spaces AND convert the Hex number into decimal at the same time.  Take a look at this thread to see how someone else had success with it:

     

    http://forums.microsoft.com/forums/ShowPost.aspx?PostID=2111360&SiteID=1

     

    Monday, September 10, 2007 4:41 PM
  •  

    Is the A0 0F allways in the same location?

     

    If so you could use Matt's code to loop through the string until you get to that location, then set your variable equal to this value.

    Monday, September 10, 2007 5:40 PM
  •  

    hi,thanks for all the reply..thanks a lot...

    By the way, where can I look for Matt's code?Is there any link provided??

    thanks a lot..

    Tuesday, September 11, 2007 1:57 AM
  •  

    Oh, sorry, Matt is klienma, his name is in his signiture, it is the post you marked as answer.
    Tuesday, September 11, 2007 1:37 PM