locked
Converting a file line string to byte array RRS feed

  • Question

  • I am trying to convert each line in a file into a byte array.  The line in the file is similar to this:

    :100000000247A1E59620E7FBE4F596900780E0FF24

    I have code and have tried several things but they don't seem to be working out.

        Dim fileline As String = ""
        Dim linebytes() As Byte = Nothing
        Dim idx As Integer
    
        ' Make sure the file is open for transferring the data to the board.
        If Not (SoftwareUpdateFilestream Is Nothing) Then
    
          ' Read and transfer one line of the file at a time.
          Do
            fileline = SoftwareUpdateStreamReader.ReadLine()
    
            If Not fileline Is Nothing Then
              ' send data
    
              'linebytes(0) = 0
    
              For idx = 0 To fileline.Length
                linebytes(idx) = StrConv(fileline(idx), VbStrConv.Narrow)
                '  linebytes(idx) = CByte(Asc(fileline(idx)))
                '  linebytes(idx) = Convert.ToByte(fileline(idx))
              Next
    
            End If
    
          Loop Until fileline Is Nothing

    Any help is appreciated.

    Thanks.

    Monday, August 30, 2010 10:04 PM

Answers

  • Thanks for the reply.

    This worked...so, do you have any ideas on what could be causing me the crashes using the other method?

    I need all characters to be converted...not just the numbers.

     

    I changed the initialization of the byte array as follows:

     

      Dim linebytes(100) As Byte

     

     

    Now I get a crash on

     

         For idx = 0 To fileline.Length
          'linebytes(idx) = StrConv(fileline(idx), VbStrConv.Narrow)
          'linebytes(idx) = Convert.ToByte(fileline.ToArray(idx), 16)
          'linebytes = instance.GetBytes(fileline)
    
    
          linebytes(idx) = CByte(Asc(fileline(idx)))
          'linebytes(idx) = Convert.ToByte(fileline(idx))
         Next
    

     

    Any of these lines where it states the array is out of bounds.  Can it be that the string variable is not initialized properly?  I'm confused because the string reads in the line of the file correctly.

     


    Thanks.

    • Marked as answer by 12AM Wednesday, September 1, 2010 7:56 PM
    Tuesday, August 31, 2010 9:48 PM

All replies

  •  

    Like this

      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        Dim Str As String = "100000000247A1E59620E7FBE4F596900780E0FF24"
        Dim ByteArray(Str.Length - 1) As Byte
    
        For index = 0 To Str.Length - 1
          ByteArray(index) = Convert.ToByte(Str.ToArray(index), 16)
        Next
    
      End Sub
    
    Monday, August 30, 2010 10:34 PM
  • Here is a fairly simple way of coding it.  It assumes that all lines are valid and the first character (i.e., the colon) can be discarded.  Note the number 16 indicating the conversion from hexadecimal.

    There are more efficient ways you could code it, but they are more complicated.  Thus, this will probably be sufficient.

    fileline = ":100000000247A1E59620E7FBE4F596900780E0FF24"
    	
    'Start at 1 to skip over the colon.
    Dim lst As New List(Of Byte)
    For i As Integer = 1 To fileline.Length - 1 Step 2
    	lst.Add(Convert.ToByte(fileline(i) & fileline(i + 1), 16))
    Next i
    
    linebytes = lst.ToArray()
    
    'Use linebytes here.
    
    Monday, August 30, 2010 10:37 PM
  • Thanks for the replies.  I keep getting an exception when I try to do any type of assignment to the linebytes array.  I don't know the string length each time because I'm reading the line string from a file.  Do I need to give an array size before attempting to assign any values to linebytes?

    Thanks.

    Tuesday, August 31, 2010 5:11 AM
  • For this is the string encoding method

    http://msdn.microsoft.com/en-us/library/ds4kkd55(VS.80).aspx

    There are much more string encoding methods than alone this one


    Success
    Cor
    Tuesday, August 31, 2010 5:27 AM
  • Maybe my problem is a bit different than it seems, because I keep getting different crashes such as:

    A first chance exception of type 'System.FormatException' occurred in mscorlib.dll

    I'm running this code (listed in my original post) on a button click.

    Something else is going on that I'm having trouble figuring out.

    Any help is greatly appreciated.

    Thanks.

    Tuesday, August 31, 2010 3:45 PM
  •  

    Because you have some caracteres are not hexadecimal, as the ":" at the begening,

    these caracteres have to be removed

    Tuesday, August 31, 2010 4:37 PM
  • Try this

    Dim bytArray() As Byte = System.Text.Encoding.ASCII.GetBytes(":100000000247A1E59620E7FBE4F596900780E0FF24")
    

    Karl ,
    Please 'Mark as Answer' if we helped.
    This helps others who have the same problem!
    Tuesday, August 31, 2010 6:32 PM
  • Thanks for the reply.

    This worked...so, do you have any ideas on what could be causing me the crashes using the other method?

    I need all characters to be converted...not just the numbers.

     

    I changed the initialization of the byte array as follows:

     

      Dim linebytes(100) As Byte

     

     

    Now I get a crash on

     

         For idx = 0 To fileline.Length
          'linebytes(idx) = StrConv(fileline(idx), VbStrConv.Narrow)
          'linebytes(idx) = Convert.ToByte(fileline.ToArray(idx), 16)
          'linebytes = instance.GetBytes(fileline)
    
    
          linebytes(idx) = CByte(Asc(fileline(idx)))
          'linebytes(idx) = Convert.ToByte(fileline(idx))
         Next
    

     

    Any of these lines where it states the array is out of bounds.  Can it be that the string variable is not initialized properly?  I'm confused because the string reads in the line of the file correctly.

     


    Thanks.

    • Marked as answer by 12AM Wednesday, September 1, 2010 7:56 PM
    Tuesday, August 31, 2010 9:48 PM