Search code examples
c#.netcp1251

C# russian cp1251 Encode/Decode


I'm using ASCII 1251 table for russian letters. So I need a C# function to convert char to 1251 decimal code and vise versa.

For example russian 'а' is 224, 219 is 'Ы'.

Are the any way not to use dictionary with hard-coded values for all the letters?


Solution

  • Just use Encoding class.

    var enc = Encoding.GetEncoding(1251);
    Console.WriteLine(enc.GetBytes("Ы")[0]); //will print 219
    Console.WriteLine(enc.GetString(new byte []{219})); //will pring Ы