Search code examples
c#decodeasciiencoding

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


Solution

  • 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?