Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

REPLACE

Posted on 2004-09-16
8
586 Views
Last Modified: 2010-04-15
Hi Experts,

I have data coming in ... but it has non-ascii characters in some places. How do I get rid of those or replace with ASCII equivalent ones if
it is possibly ?

Thank you.
0
Comment
Question by:fpoyavo
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 7

Expert Comment

by:CJCraft
ID: 12080581
The following code snippet should give you the idea.

The idea is go through the string and see if it ascii character value in in range of values you consider valid.
You may need to post more details before you get the answer you are after.

byte[] bScrubbed = new byte[(int)dwBytesRead];
int nPos = 0;

// Scrub the non-ascii characters
for (int i = 0; i < (int)dwBytesRead; i ++)
{
   if (((int)bData[i] > 19) && ((int)bData[i] < 125))
   {
      bScrubbed[nPos] = bData[i];
      nPos++;
   }
}
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 12080840
you can do this using the ASCII encoding class ... I can give you specific code to handle the case if you can tell me what encoding you are coming from (i.e. UTF 16) ...
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 12083120
Gregory,

Yep. Its UTF 16.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:tzxie2000
ID: 12083241
suggest the data you receive is correct data but in UTF16 and it saved in s[]

char [] s;

//some code to receive data in s

Encoder UTFEncoder = Encoding.UTF16.GetEncoder();

int byteCount = UTFEncoder.GetByteCount(s, 0, s.Length, true);
Byte[] bytes = new Byte[byteCount];
int bytesEncodedCount = UTFEncoder.GetBytes(s, 0,s.Length, bytes, 0, true);
//ok it change to UTF16
// bytesEncodedCount is the real changed bytes number
 Console.WriteLine("{0} bytes used to encode characters.", bytesEncodedCount );
//show the encoded bytes
Console.Write("Encoded bytes: ");
  foreach (Byte b in s) {
        Console.Write("[{0}]", b);
   }





UTFEncoder


0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 12084127
once you have them in a byte array as displayed by tzxie2000 you can then get them into an ASCII string by using the ASCII encoder object .GetString() method.
note that null characters cause issues with strings in .net so they should be removed prior to this process (C style strings)
0
 
LVL 10

Accepted Solution

by:
ptmcomp earned 500 total points
ID: 12091194
string result = Regex.Replace(input, @"[^\x20-\x7e]", "");  // removes all chars that are not in the range 0x20 - 0x7e
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 12094093
ptmcomp,

You are good. How about to replace using regex any special character with \\special character ?
Example :  $    to   \\$

Thank you.
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 12095124
I'm not sure if I understand your question.
If you want to replace "$" by "\\$" and let's say "@" by "\\@" then you could use this:  
string result = Regex.Replace(input, @"[$@]", @"\\$0");
Note: "$0" stands for the string matched by the expression
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question