none
Convert Array in String to Array in Byte RRS feed

  • Question

  • Hi,

    I have an array defined As string and the value stored in site is like "7E", "A1" and so on. But in order to send out this array through serial port. I need to change the the array to As Byte instead of using As String. How can I convert it? Please advice.

    Thanks
    Wednesday, October 7, 2009 9:59 AM

Answers

  •             string[] myString = { "7E", "A1" };
                byte[] myBytes = Array.ConvertAll<string, byte>(myString,(strHold) => Convert.ToByte(strHold, 16));

    Or in VB.Net:

            Dim myString() As String = {"7E", "A1"}
            Dim myBytes() As Byte = Array.ConvertAll(Of String, Byte)(myString, Function(strHold) Convert.ToByte(strHold, 16))
    Wednesday, October 7, 2009 5:09 PM

All replies

  • Hi Cor,

    Can you provide me some example on how I can implement the function.

    Thanks

    Wednesday, October 7, 2009 10:33 AM
  • By the way, what I intend to do is to move whatever stored in my string array to byte array without changing the value in string format because that is the value I want to send out through serial port.
    Wednesday, October 7, 2009 10:36 AM
  • Anyone with any ideas?
    Wednesday, October 7, 2009 4:06 PM
  • try this link:


    http://blogs.techrepublic.com.com/programming-and-development/?p=415


    also, if you Google vb.net string to byte array, there are a lot of examples to look at
    Wednesday, October 7, 2009 4:37 PM
  • Twp clicks and this code is in the link

    Imports System
    Imports System.Text
    Imports Microsoft.VisualBasic.Strings
    
    Class EncoderExample
        
        Public Shared Sub Main()
            Dim bytes() As Byte
            ' Unicode characters.
            ' ChrW(35)  = #
            ' ChrW(37)  = %
            ' ChrW(928) = Pi
            ' ChrW(931) = Sigma
            Dim chars() As Char = {ChrW(35), ChrW(37), ChrW(928), ChrW(931)}
            
            Dim uniEncoder As Encoder = Encoding.Unicode.GetEncoder()
            
            Dim byteCount As Integer = _
                uniEncoder.GetByteCount(chars, 0, chars.Length, True)
            bytes = New Byte(byteCount - 1) {}
            Dim bytesEncodedCount As Integer = _
                uniEncoder.GetBytes(chars, 0, chars.Length, bytes, 0, True)
            
            Console.WriteLine( _
                "{0} bytes used to encode characters.", _
                bytesEncodedCount _
            )
            
            Console.Write("Encoded bytes: ")
            Dim b As Byte
            For Each b In  bytes
                Console.Write("[{0}]", b)
            Next b
            Console.WriteLine()
        End Sub 'Main
    End Class 'EncoderExample

    Success
    Cor
    Wednesday, October 7, 2009 4:55 PM
  •             string[] myString = { "7E", "A1" };
                byte[] myBytes = Array.ConvertAll<string, byte>(myString,(strHold) => Convert.ToByte(strHold, 16));

    Or in VB.Net:

            Dim myString() As String = {"7E", "A1"}
            Dim myBytes() As Byte = Array.ConvertAll(Of String, Byte)(myString, Function(strHold) Convert.ToByte(strHold, 16))
    Wednesday, October 7, 2009 5:09 PM
  • Hi All,

    Thanks for all the response. Eventually, Syntaxeater's code works well and it is in a simplest form, 1 LINE. Thanks bro.

    Thursday, October 8, 2009 1:56 AM