Rhyono
Rhyono

Reputation: 2468

C++ equivalent of JS .charCodeAt()

It's probably something so extremely obvious and simple that my attempts at searching for it have failed, but: using glib you can take user input of Unicode, similar to the way I can just paste this here. I would like to be able to convert that to a string of its hex equivalent, e.g. the way "亜".charCodeAt(0).toString(16) returns 4e9c. Is there such a standard function for doing that, a method with glib, or some other library that will do it?

Upvotes: 3

Views: 2226

Answers (1)

nemequ
nemequ

Reputation: 17482

With glib you can get a single Unicode code point (a gunichar) using g_utf8_get_char. If you need a non-zero offset, just pass the return value of g_utf8_offset_to_pointer to g_utf8_get_char. For the conversion to string, you can just use g_strdup_printf.

So, putting it all together the translation of the code you posted would be:

g_strdup_printf ("%x", g_utf8_get_char (g_utf8_offset_to_pointer ("亜", 0)));

Upvotes: 3

Related Questions