Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

Non Printable Characters using C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greyhound - 10 Apr 2008 21:20 GMT
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 - 10 Apr 2008 21:54 GMT
> 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.

Signature

J.

Jon Skeet [C# MVP] - 10 Apr 2008 23:04 GMT
> 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
Arne Vajhøj - 11 Apr 2008 00:36 GMT
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.