tags:

views:

93

answers:

4

How can I specify the Unicode character INFINITY (U+221E) in C++ without directly pasting the symbol (∞) into my code file? I've tried \x221e but that results in a warning, and \u221e gives me a LATIN SMALL LETTER A WITH CIRCUMFLEX (U+00E2).

QString label;
label.append(tr("HP: \u221e\n\n"));
A: 

The Unicode escape is the correct way, but something else along the path is breaking it and displaying the UTF-8 as Latin-1.

Ignacio Vazquez-Abrams
'\u221E' is not UTF-8 encoded.
Ben Voigt
@Ben: Of course it isn't. Something else is doing the right thing and encoding it as `0xE2 0x88 0x9E`. But something *else* is doing the *wrong* thing and displaying it as Latin-1.
Ignacio Vazquez-Abrams
A: 

That's a 16-bit Unicode character. 16-bit characters can only be expressed as a single element if you are using a wide string.

Try

L"HP: \x221E\n\n"

or

u"HP: \x221E\n\n"

What encoding are you using? If QT is expected UTF-8, then you'll actually need a sequence of multiple bytes to encode that character.

Ben Voigt
L should prefix the literal, not postfix it
usta
Thanks usta, just caught that myself checking the C++ syntax.
Ben Voigt
+5  A: 

Try \xE2\x88\x9E.

But to make Qt use UTF-8, it seems you'll need

QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));

Somewhere before it.

usta
Exactly what I needed, thanks!
Jake Petroules
A: 

This could be used as an alternative as well:

label.append(trUtf8("HP: \u221e\n\n"));
Arnold Spence