1

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?

Punk1503
  • 75
  • 1
  • 6

1 Answers1

2

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 Ы
Serg
  • 3,454
  • 2
  • 13
  • 17
  • can you please explain this getBytes() and why do you use [0]. If I pass the whole word or sentence to this function, will I get an array of integers? Like `enc.GetBytes("ЫЫЫЫЫ")` gives me [219, 219, 219, 219, 219]. And the most weird thing is that `new byte`. What does that mean? I understand that we are creating a new byte object array, but can i pass a lot of values to this { some values } – Punk1503 Apr 05 '21 at 18:27
  • 1
    `GetBytes` will return byte a array. So, I just get first byte to show exact output for single char. If you will pass the whole word, then you will get the bytes for whole word (so, your example is correct). The `new byte[]{219}` is just an array declaration with initialization, so it is a byte array of one element. You can definitely pass many values like this: `new byte[]{1, 2, 3, etc}`. You can also just pass an array, created separately. – Serg Apr 05 '21 at 18:35
  • Thanks a lot for a proper explanation – Punk1503 Apr 05 '21 at 18:36