简体   繁体   中英

C# Decode Cyrillic to ASCII

I have a very nice function which encodes ASCII to russian charachters, however I need it also the other way around from russian to ASCII.

The function I have is:

public string DecodeEncodedNonAsciiCharacters(string value)
    {
        return Regex.Replace(
            value,
            @"\\u(?<Value>[a-zA-Z0-9]{4})",
            m =>
            {
                return ((char)int.Parse(m.Groups["Value"].Value, NumberStyles.HexNumber)).ToString();
            });
    }

I cant find a good way to get \\u235 in my text or any other way to escape these type of characters

Something like this? (Fiddle: https://dotnetfiddle.net/6BbXAt )

public static string EncodeNonAsciiCharacters(string value)
{
  return Regex.Replace(
    value,
    @"[^\x00-\x7F]",
    m => String.Format("\\u{0:X4}", (int)m.Value[0]));
}

The regex is from (grep) Regex to match non-ASCII characters?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM