Author Topic: Converting byte array to char array  (Read 1417 times)

bachphi

  • Hero Member
  • *****
  • Posts: 690
    • View Profile
Converting byte array to char array
« on: July 21, 2016, 09:48:06 PM »
I'd like to convert byte array to char array. Below is what I have:
Code: [Select]
       Dim nbytes As Integer = COMPort.BytesToRead
        Dim comBuffer() As Byte = New Byte(nbytes - 1) {}        'create a byte array to hold the awaiting data
        COMPort.Read(comBuffer, 0, nbytes)                       ' {179,254,128,25,170,12}
        Dim charArray() As Char
        Dim strTemp As String = ""
        strTemp = BitConverter.ToString(comBuffer).Replace("-", " ")    'BE FE 80 19 AA 0C
        charArray = System.Text.Encoding.ASCII.GetString(comBuffer).ToCharArray()
I can do charArray = strTemp.ToCharArray() but would like to avoid using strTemp if possible.
I need charArray looks to be the same strTemp that is 'BE FE 80 19 AA 0C'. Thanks.
« Last Edit: July 21, 2016, 10:08:23 PM by bachphi »
UCL =================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
My understanding of computer is well below six Σ.
Unless what I am saying is logically defined in a PLC, everything else might be beyond my control.
LCL =================

bachphi

  • Hero Member
  • *****
  • Posts: 690
    • View Profile
Re: Converting byte array to char array
« Reply #1 on: July 21, 2016, 10:11:02 PM »
I got it:

charArray = BitConverter.ToString(comBuffer).Replace("-", " ").ToCharArray()
UCL =================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
My understanding of computer is well below six Σ.
Unless what I am saying is logically defined in a PLC, everything else might be beyond my control.
LCL =================