locked
Convert string of byte into a list of byte [VB.NET] RRS feed

  • Question

  • Hi,

    I've got this :

    Dim MyString As String = "&H80, &H82, &H60"
    

    And i want to get this result:

    Dim TestData() As Byte = {&H80, &H82, &H60}

    The string can be : "80 82 60" or "80.82.60 if we want. But i want to have this result.

    How ?

    Please, can somebody help me ?

    Louis



    Saturday, February 7, 2015 9:59 PM

Answers

  • If you can get rid of the &H then you can parse it a little easier, but this should work for that exact string:

    Dim MyString As String = "&H80, &H82, &H60"
    Dim TestData() As Byte = MyString.Split(","c).Select(Of Byte)(Function(s) Byte.Parse(s.TrimStart("&"c, "H"c, " "c), Globalization.NumberStyles.AllowHexSpecifier)).ToArray
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Luigi21 Saturday, February 7, 2015 10:25 PM
    Saturday, February 7, 2015 10:12 PM

All replies

  • If you can get rid of the &H then you can parse it a little easier, but this should work for that exact string:

    Dim MyString As String = "&H80, &H82, &H60"
    Dim TestData() As Byte = MyString.Split(","c).Select(Of Byte)(Function(s) Byte.Parse(s.TrimStart("&"c, "H"c, " "c), Globalization.NumberStyles.AllowHexSpecifier)).ToArray
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Luigi21 Saturday, February 7, 2015 10:25 PM
    Saturday, February 7, 2015 10:12 PM
  • Hi

    Maybe this?

            Dim MyString As String = "&H80, &H82, &H60"
            Dim TestData() As String = MyString.Split(","c)
    If you use a different separator, you would need to change the Split character accordingly.


    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, February 7, 2015 10:20 PM
    Saturday, February 7, 2015 10:13 PM
  • Thank you, it works !! ;)

    But i don't understand the code, can you explain me the code please?

    Saturday, February 7, 2015 10:33 PM
  • Hi

    It is a very simple piece of code. It takes the original string and makes an array from it by splitting it at the separator (in the case above the separator is a comma but you can have any separator).

    Edit: ah OK you were talking to someone else. I hadn't noticed there was an answer before mine.


    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, February 7, 2015 10:43 PM
    Saturday, February 7, 2015 10:41 PM
  • Thank you, it works !! ;)

    But i don't understand the code, can you explain me the code please?

    First the Split() method is used to separate the string at each comma into an array of strings.

    Then the Select() LINQ extension method is called to execute a function against each sub string in the split array.

    The Select() method takes an in-line Function which has a single string parameter.  The value of this string is first trimmed to remove any &, H or space character and then it is parsed into a byte from a Hex-string format.

    This results in a LINQ enumeration which is then returned as an Array of bytes.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, February 7, 2015 10:45 PM
  • Here is the same idea spread out across multiple lines of code.  This may make the execution more clear:

    Dim result As New List(Of Byte)
    For Each subString As String In MyString.Split(","c)
        result.Add(ParseByte(subString))
    Next
    
    Private Function ParseByte(s As String) As Byte
        Return Byte.Parse(s.TrimStart("&"c, "H"c, " "c), Globalization.NumberStyles.AllowHexSpecifier)
    End Function
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, February 7, 2015 10:50 PM