Accentuated character strings

Hello,

I know this problem has already been discussed but i can’t find any solution.

I need to display special (accentuated) characters but compiler complains with a warning : illegal character encoding in string literal and the character is not displayed.

font.drawString("î é", 300, 550);

i know this is a character encoding issue, but how could i solve it ?

thanks a lot

Hi, I’m a spanish speaker (despite of my name), so Ive faced this a lot.
You cant put accentuated characters in the code. :frowning:
What I usually do is to pull the string from an external file, like an xml. This also allows me to keep these strings tidy, edit them without recompiling and eventual translations.
Use ofXml. It is super easy and very few lines of code.
say you have an xml file named mystrings.xml in your apps data folder, that looks like this.

<myStrings>
	<aString>áéíóúäñ</aString>
</myStrings>

and the code should be

	ofXml xml;
    string aString;
	string xmlPath = "mystring.xml";
	if (xml.load(xmlPath)) {
		aString = xml.getValue("aString");
	}else{
    //The following code is not really needed but it is handy as it will create the xml file for you if it doesn't exist and format it correctly. It is much better, safer and faster than formatting by hand. Then just edit the values in the xml file.

	xml.addChild("myStrings");
	xml.setTo("myStrings");
	xml.addValue("aString", "this is a placeholder string. edit in the xml file.");
	xml.save(xmlPath);
	aString = "";
}

I think that you might be able to put accentuated characters using its utf8 code or something. Not really sure. Maybe @arturo has a better answer for this.

cheers

if you set the encoding of the cpp or .h to utf-8 that should work without problem

well, this is interesting… but how would you achieve that ?

it depends on where you are editing the file but usually when saving there’s an option to choose the encoding. in most IDEs and modern code editors i think the default is already utf8 but i think xcode defaults to utf32 or something like that

2 Likes

so you say i need to edit the .h or .cpp file and save it to UTF8 encoding to make it to work !?

Damn ! This is working !

I didn’t realized this was the source file that was issuing the problem, i thought this was some library that couldn’t handle this character encoding…

Anyway, i edited it and re-saved it using Coteditor and everything seems ok now
Strange though, because xCode is set to save files into UTF8

Thanks everybody !

Have a nice evening (depending where you are located :wink: )

Ha! great tip @arturo! Muchas gracias.

I do end up with strange characters in my code though

font.drawString("î é", 300, 550);

what if you change them back and save once you’ve set the encoding? have never tried it in xcode but it usually works right away in other IDEs / editors which is usually the default. you can also try opening that specific file in another editor

No luck. I changed the characters back and saved but the problems appears again.

For the record, the .cpp file shows as Western (Mac Roman) encoding when opened in Coteditor

Just one note, every time you update your project with project generator in XCode, it will reset the encoding of the file back to Western (or whatever your default is). When you switch encoding, just set use “reinterpret” rather than convert. It always sticks for me once I save it, but if I regenerate, it resets encoding, causing problems.

related:

https://github.com/openframeworks/projectGenerator/issues/111
https://github.com/openframeworks/openFrameworks/issues/4728

1 Like