0

I have the following string:

string str = "Здравей!";

I have to pass it to a printer to print it. The printer method to print is datecs_test.PrintTaggedText(str); But on print I don't get the same string content but some strange symbols. They told me that before I pass str to datecs_test.PrintTaggedText(str); I have to convert str to win-1251. But all solutions I find doesn't seem to work.

  • 4
    You try obtaining bytes `byte[] data = Encoding.GetEncoding(1251).GetBytes(str);` which then pass to `datecs_test` – Dmitry Bychenko May 24 '18 at 13:57
  • 3
    If `PrintTaggedText` accepts a `string` but expects it to be anything other than UTF-16, then it is not a good method and it must be fixed, because `string` means "a sequence of UTF-16 chars". If `PrintTaggedText` accepts a `byte[]`, then I find it difficult to believe that you even looked for solutions. – GSerg May 24 '18 at 14:01
  • Dmitry Bychenko but the method `datecs_test.PrintTaggedText(str)` expects a string – ProfileForStack4 May 24 '18 at 14:02
  • 1
    Whenever you work with a `string` in C# it will always be a sequence of [UTF-16 chars](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/char) -- as shown, you can get a `byte[]` *representation* of it in a different encoding, but a `string` will always be a `string` (and always in UTF-16). – Corak May 24 '18 at 14:04
  • 1
    @ProfileForStack4 In that case what you are being told to do doesn't make much sense. A `string` cannot represent a `win-1251` value; it can only be converted into bytes representing a `win-1251` value. Do you have any documentation on this library? – JLRishe May 24 '18 at 14:04
  • I don't understand, so how do I convert the string to `win1251` so I can pas it to the method. – ProfileForStack4 May 24 '18 at 14:05
  • 1
    You don't. If you have a method signature on the C# side that accepts a `string`, it has to be a normal .NET UTF-16 string. If the printer does not understand that, then the code inside `PrintTaggedText` should manage the conversion. Obligatory reading: http://www.joelonsoftware.com/articles/Unicode.html – GSerg May 24 '18 at 14:07
  • the printer has a method which accepts `byte[]` I saw it now `datecs_test.PrintText(data);` . Thanks! – ProfileForStack4 May 24 '18 at 14:10
  • 1
    Printers like that (thermal printers that use ESC codes) in general have their own device fonts. You have to send anther command to switch the character set. Look in the programming manual for the command, ensure it actually has a font that can display Cyrillic glyphs. – Hans Passant May 24 '18 at 14:15

0 Answers0