C++ string/char and accents

与世无争的帅哥 提交于 2019-12-13 01:25:36

问题


I'm writing a text writer in C++, which I'll have a string of a phrase and display the appropriate bitmap font for each char value.

For now, it's working for the regular characters, but I'm getting weird values for accents and other characters such as À, Á, Â, Ã, etc

I'm doing this:

int charToPrint = 'a';
//use this value to figure which bitmap font to display

The bitmap font does have these characters, but on this line I'm not getting the values I'm supposed to get, such as: 195 for Ã, 199 for Ç, etc...

I tried changing my project's character set from Multi Byte to Unicode, but I don't think that does anything for the char->int conversion...

How can I get this conversion with chars?

Edit: I'm using Visual Studio 2012, Windows 7, and it's an OpenGL application with a bitmap font. I've mapped the positions/width/height of each character, according to it's char value, so the character a is at the position 97 of my bitmap font (plus width accounted for).

To draw, I just need to figure the position based on the char code.

I have a string of a phrase I want to display, and I loop through each character, figure the charCode, and call my draw function.

For these characters with accents, I'm getting negative values, so my draw function doesn't do anything (there's no position -30 for Ç for example). I need to figure how to get these values properly and send to the draw function.


回答1:


Use Unicode, it is year 2013 already :) The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets

You will use wchar_t as a type and UTF-16 / UTF-32 encoding. That will make your code supporting not only "irregular" characters but many more "irregular" characters :) (there is no such a thing as regular characters).

Example

wchar_t c = L'Á';
printf("char: %lc encoding: %d\n", c, c);

c = 0xc1; 
printf("char: %lc encoding: %d\n", c, c);

Output

char: Á encoding: 193
char: Á encoding: 193


来源:https://stackoverflow.com/questions/18819861/c-string-char-and-accents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!