• ASCII character as delimiter in txt file (.NET framework 1.1)

    Home » Forums » Developers, developers, developers » DevOps Lounge » ASCII character as delimiter in txt file (.NET framework 1.1)

    Author
    Topic
    #418129

    Dear Loungers,
    I’m reading a text file generated by legacy app, and writing code to parse it into fields. The file uses a delimiter that cannot by typed at the keyboard, so it appears as a weird-looking character in the stream. The legacy programmer says its “~001”.

    // this does not work
    string [] HoursWorkedFields = System.Text.RegularExpressions.Regex.Split(line,”01″);

    This is what is returns (paste from Immediate window): ? HoursWorkedFields[0]

    “00065ALLAM2510197300 40.00”

    How can I represent the character as a delimiter? How can I test a char to find out what its ASCII code is?

    Any help gratefully received,

    Viewing 1 reply thread
    Author
    Replies
    • #940085

      The Microsoft.VisualBasic namespace gives you access to the Chr() function. Try Chr(001) as your delimiter. You can also try Chr(1) and Chr(01), both of which also produce the box character. You may have to experiment to figure out the right ascii code to use for the specific delimiter used in the legacy app, since it isn’t as easy to back into in .Net as it is in VB/VBA.

    • #940148

      Hi Gwenda,

      Several thoughts come to mind:
      1) Since you have access to the legacy programmer, do you also have access to the legacy source code? If so, that *may* be of some use to finding the exact characters of the mystery delimiter.

      2) The character is not a specific characeter. It’s often used when the actual character can not be displayed for whatever reason. I’m not sure exactly what the reason is, but I often see this character in database text fields representing a line break/carriage return when viewing the raw data in a table.

      2) You can possibly determine the string value by isolating the characters in question and using the following code snippet:

              'Note: This is very crude, but will return the ascii characters for any given string
              Function GetAsciiCharacterCode(ByVal input As String) As String
              Dim returnString As String 'String to return
              Dim encodingObject As System.Text.Encoding = System.Text.Encoding.Default 'Encoding object
      
              'Get byte array from input string
              Dim encodedArray As Byte() = encodingObject.GetBytes(input)
              Dim b As Byte 'Byte object used for iteration
      
              'For each byte in encodedArray
              For Each b In encodedArray
                  'Append byte's string value to return string
                  returnString &= b.ToString() & Environment.NewLine
              Next
      
              'return
              Return returnString
          End Function

      This will return a string of the Ascii character codes separated by line breaks.

      Let me know if you have any questions or problems with this. Hope this helps!

      • #940172

        Thanks MarkJ and Charlotte, I’ll try these ideas and let you know how it works.

      • #941769

        Hi Mark and Charlotte,
        I talked to the legacy programmer and confirmed that the ASCII delimiter was 01 (that was a good idea Mark). Then investigating Regular Expressions a bit more, I found that I needed to include an “x” after the “” escape code to indicate that an ASCII character follows. So this now works:

        string [] HoursWorkedFields = System.Text.RegularExpressions.Regex.Split(line,”x01″);

        Regular Expressions are wicked!

    Viewing 1 reply thread
    Reply To: ASCII character as delimiter in txt file (.NET framework 1.1)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: