Skip to main content
Alex Golubev
Associate III
January 14, 2021
Question

sprintf function and Cyrillic

  • January 14, 2021
  • 11 replies
  • 2607 views

I ran into the following problem the sprintf(str, "привет") function; encodes in a two-byte format. Each even character = 208 (dec).

str[0]	char	208 '�?'	
str[1]	char	159 '\237'	
str[2]	char	209 'Ñ'	
str[3]	char	128 '\200'	
str[4]	char	208 '�?'	
str[5]	char	184 '¸'	
str[6]	char	208 '�?'	
str[7]	char	178 '²'	
str[8]	char	208 '�?'	
str[9]	char	181 'µ'	
str[10]	char	209 'Ñ'	
str[11]	char	130 '\202'

    This topic has been closed for replies.

    11 replies

    Peter BENSCH
    ST Technical Moderator
    January 14, 2021

    Привет, Alex,

    this is because Cyrillic is not part of the ASCII character set, but rather of Unicode and therefore requires two bytes per character.

    И�?кренне Ваш

    /Peter

    In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
    Alex Golubev
    Associate III
    January 14, 2021

    I need a single-byte mode with Cyrillic characters. How can I do this?

    Pavel A.
    January 15, 2021

    Use single byte encoding such as CP866 https://en.wikipedia.org/wiki/Code_page_866

    But to display it you'll need a terminal that can render it.

    -- pa

    Alex Golubev
    Associate III
    January 16, 2021

    Pavel A. How do I add cp866 to stm32cubeide?

    Pavel A.
    January 16, 2021

    I don't know. Eclipse once supported non-unicode encodings, you can ask in Eclipse mailing list how to get it back.

    You even do not have to use Eclipse as editor, use whatever works.

    The gcc compiler should accept one byte encoding if you don't coerce it to interpret the source as unicode (with L"..." or u8"..." strings)

    Another option - do not use cyrillic in your C source files, keep the Cyrillic strings as "resources" elsewhere.

    -- pa

    Alex Golubev
    Associate III
    January 16, 2021

    I tried adding the command gcc-fexec-charset=CP1251 to project ---> properties ---> c/c++ build ---> settings ---> MCU C Compiler ---> gcc -fexec-charset=CP1251.

    Got errors

    make: *** [Src/subdir.mk:89: Src/main.o] Error 1
    make: *** Waiting for unfinished jobs....

    Pavel A.
    January 16, 2021

    Do not add -fexec-charset=CP1251. Just compile as is. Source with CP866 strings compiles for me with gcc v7 in Linux.

    Alex Golubev
    Associate III
    January 16, 2021

    I'm a novice programmer. Explain in more detail. How did you enable cp866?

    Pavel A.
    January 17, 2021

    0693W000007BZAGQA4.jpg

    Pavel A.
    January 17, 2021

    Maybe both source and target charsets for gcc should be specified:

    gcc -finput-charset=CP866 -fexec-charset=CP866

    Depends on the default with which gcc was built.

    I tried also to pretend that both charsets are UTF-8 (though source is CP866) ; it seems to work.

     gcc -finput-charset=utf-8 -fexec-charset=utf-8

    0693W000007BZFzQAO.jpgOutput:

    $ gcc -finput-charset=UTF-8 -fexec-charset=utf-8 ***.c

    $ ./a.out

    [0] AF

    [1] E0

    [2] A8

    [3] A2

    [4] A5

    [5] E2

    [6] 00

    strlen=6

     As you see the string is correctly encoded in 866, each char one byte.

    -- pa

    Alex Golubev
    Associate III
    January 28, 2021

    How to add cp866 to the debugger?