cancel
Showing results for 
Search instead for 
Did you mean: 

sprintf function and Cyrillic

Alex Golubev
Associate II

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'

11 REPLIES 11
Peter BENSCH
ST Employee

Привет, 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 II

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

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 II

Pavel A. How do I add cp866 to stm32cubeide?

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 II

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.
Evangelist III

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 II

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

0693W000007BZAGQA4.jpg