Output Unicode Number?

How do I output a Unicode number if I extract a character off a console or out of a file.

If I do the below, I use the Unicode number to show a character. The below shows me 25² .

1
2
3
char b = '\u00B2';
mystring = "25";
mystring.append(1,b);


How do you go back the other way?

If I extract the 25 and the ² separately, how do I get the unicode number for ² ?

The char data type already is a number equal to the value of whatever character it contains. However, I get a warning with g++ that '\u00B2' doesn't fit in a char.

http://coliru.stacked-crooked.com/a/56f52ddeb1bbfd59
Thanks, that works. I'm not using C++11 though so your code won't run on my compiler.

1
2
3
4
5
6
7
8
string s = "\u00B2";
int ln = s.length();

for (int i = 0; i < ln;i++)
{
    cout  << std::hex  << static_cast<unsigned int>(s[i]) << endl;

}
char cannot hold unicode points. What happened is a lucky combination of implementation depended behavior and current locale.

It warns about result not fiting in char, and about multicharacter literal (Useless backward compatibility feature from C)

'\u00B2' is not char, but int.
Topic archived. No new replies allowed.