W
W
wizikos2014-11-26 15:51:21
JSON
wizikos, 2014-11-26 15:51:21

How to convert Russian text to \u0418\u043b\u044c\u044f?


Tell me how to translate plain Russian text to the form \u0418\u043b\u044c\u044f using C# or JS .
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav, 2014-11-26
@mastan

using System;
using System.Text;

class Program
{
    static void Main()
    {
        string st = "Привет, мир!";
        Console.WriteLine(foo(st));
    }
    
    static string foo(string input)
    {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < input.Length; i += Char.IsSurrogatePair(input, i) ? 2 : 1)
        {
            int index = Char.ConvertToUtf32(input, i);
            sb.Append(@"\u" + index.ToString("x4"));
        }
        
        return sb.ToString();
    }
}

For surrogate pairs, it will produce "ugly" values ​​- there will be more than four digits.
If you plan to work with them, you need to come up with something. For example, for all the format is "x8".
If not, it's better to throw an exception on if(Char.IsSurrogatePair(input, i)).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question