-3

I need a function that returns the UNICODE value of a character.
In java String.valueOf('а'); is valid.
Is there equivalent function in c++?

Temp Temp
  • 121
  • 1
  • 1
  • 3
  • 2
    A UNICODE character is (possibly) just a `uint16_t` anyway. – Roger Rowland Jul 15 '13 at 08:16
  • 3
    @RogerRowland So what about utf 32? also utf8, not exactly a `uint16_t`. – Twifty Jul 15 '13 at 08:18
  • @Waldermort lol, as I finished typing I wondered who would open that can of worms! – Roger Rowland Jul 15 '13 at 08:18
  • @RogerRowland lol. Trust me, all this unicode, locale and whatnot have been doing my head in these last few days. – Twifty Jul 15 '13 at 08:21
  • 1
    Errr... define "character". A unicode character is a unicode character, and its value is itself. Unless you mean something like "Unicode mnemonic", like "LATIN CAPITAL LETTER A". – Kerrek SB Jul 15 '13 at 08:23
  • One possibility would be to use [`btowc`](http://en.cppreference.com/w/cpp/string/multibyte/btowc) (though I'm uncertain enough about what you want, that it's essentially impossible to say for sure). – Jerry Coffin Jul 15 '13 at 08:29
  • @JerryCoffin The only thing he tells us is what he would do in Java. For which the exact equivalent in C++ is `u16string( 1, u'a' )`. (Whether that's what he really wants, however, is unclear; his text suggests that he doesn't actually know what the Java function does.) – James Kanze Jul 15 '13 at 08:31

1 Answers1

2

The question really doesn't make sense in the context of C++. You have to define what you mean by the Unicode value (which encoding format)—Java insists on UTF-16, but arguably UTF-8 or UTF-32 make more sense.

Also, despite the name, the Java function does not change or reinterpret any values. It's the exact equivalent of wstring( 1, L'a' ) in C++, at least on machines where wchar_t is UTF-16. (In C++11, you can force UTF-16, with std::u16string( 1, u'a' ).)

James Kanze
  • 150,581
  • 18
  • 184
  • 329