Non Printable Characters using C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?R3JleWhvdW5k?=

    Non Printable Characters using C#

    I need to remove non-printable characters from a text file. I need to do
    this in C#. The Hex codes for the characters I need to remove are '0C' and
    '0A' which equate to 12 and 10 in decimal. Their codes are 'FF' and 'LF'
    from the ASCII talbe. I have searched and searched and cannot come up with a
    method for doing this.

    Many Thanks...
  • Jeroen Mostert

    #2
    Re: Non Printable Characters using C#

    Greyhound wrote:
    I need to remove non-printable characters from a text file. I need to do
    this in C#. The Hex codes for the characters I need to remove are '0C' and
    '0A' which equate to 12 and 10 in decimal. Their codes are 'FF' and 'LF'
    from the ASCII talbe. I have searched and searched and cannot come up with a
    method for doing this.
    >
    Use a FileStream to read the existing file and a FileStream for creating the
    new file. .Read() from one and .Write() to the other, skipping the unwanted
    characters. A BufferedStream may improve performance.

    Anything more and I'd be writing the code for you, and that would be bad.

    --
    J.

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Non Printable Characters using C#

      On Apr 10, 1:20 pm, Greyhound <rhill...@hotma il.comwrote:
      I need to remove non-printable characters from a text file. I need to do
      this in C#. The Hex codes for the characters I need to remove are '0C' and
      '0A' which equate to 12 and 10 in decimal. Their codes are 'FF' and 'LF'
      from the ASCII talbe. I have searched and searched and cannot come up with a
      method for doing this.
      Just read the text, then remove the appropriate characters using
      something like string.Replace. The escape code for FF is "\f" and for
      LF it's "\n".

      Jon

      Comment

      • =?UTF-8?B?QXJuZSBWYWpow7hq?=

        #4
        Re: Non Printable Characters using C#

        Greyhound wrote:
        I need to remove non-printable characters from a text file. I need to do
        this in C#. The Hex codes for the characters I need to remove are '0C' and
        '0A' which equate to 12 and 10 in decimal. Their codes are 'FF' and 'LF'
        from the ASCII talbe. I have searched and searched and cannot come up with a
        method for doing this.
        s = Regex.Replace(s , "[\f\n]", "");

        or maybe:

        s = Regex.Replace(s , "[\u0000-\u001F]", "");

        Arne

        Comment

        Working...