For example, the following code should return the string "G":

Encoding.ASCII.GetString((new byte[] { 195 })); 

But why does the string return: "?".

As in C #, it is correct to convert an array of bytes to an ASCII string encoding with rus. characters?

  • ASCII has codes 0-127. So 195 can never be ASCII. This may be some kind of national encoding. - Alexander Petrov

2 answers 2

ASCII is only 7-bit - no values ​​above 127. Does the encoder usually decode unknown binary values ​​into ? (although this can be changed with the Decoder Fallback ).

Next, to get the letter G, indicate that you want Cyrillic (1251):

  public class Program { public static void Main(string[] args) { Console.WriteLine(System.Text.Encoding.GetEncoding(1251).GetString((new byte[] { 195 }))); } } 
  • school497.ru/download/u/02/les10/les.html (for example) Some sources indicate that ASCII has the "second half". Who to believe?) But: "Only the first half of the table is an international standard, that is, characters with numbers from 0 (00000000) to 127 (01111111)." - XXX
  • Counter-example: on my computer it is English and French, and for Cyrillic I always have to specify the encoding directly. As @AlexanderPetrov specified, Default depends on the machine. - Serhii S.
  • @XXX - this can be asked as a separate question (I'll add). That the reference answer was received from the guru to which it will be possible to refer then. - Alexander Petrov

The international standard is only the first half of the table, i.e. characters with numbers from 0 (00000000), up to 127 (01111111)

Alternative part of the table (Russian). The second half of the ASCII code table, called the code page (128 codes starting from 10,000,000 and ending with 11111111), can have different options, each option has its own number. The code page is primarily used to place national alphabets other than Latin. In Russian national encodings in this part of the table are placed the characters of the Russian alphabet.

The following code works as it should, if the correct localization (for us) is specified on the machine:

 byte[] Test = new byte[] { 195 }; Encoding.Default.GetString(Test); 

But! Using Default is a dangerous way. It breaks down on a machine where another localization is currently used.

  • 2
    Using Default is a dangerous way. It breaks down on a machine where another locale is currently being used. - Alexander Petrov