Re: int (utf-8) to Glib::ustring
- From: Kjell Ahlstedt <kjell ahlstedt bredband net>
- To: "Spazzatura.Live" <kharhonte hotmail com>
- Cc: gtkmm-list gnome org
- Subject: Re: int (utf-8) to Glib::ustring
- Date: Sun, 04 Dec 2011 13:40:26 +0100
2011-12-03 15:29, Spazzatura.Live skrev:
char *utf8hex_to_str(const char *utf8hex)
{
const size_t utf8_char_size_t = strlen(utf8hex)/2 + 1;
char *utf8_char = new char[utf8_char_size_t];
int utf8int;
sscanf(utf8hex, "%8x", &utf8int);
utf8_char[utf8_char_size_t-1] = '\0';
for(size_t i=0;i<utf8_char_size_t-1;i++)
{
utf8_char[i] = (char)(utf8int >> (8*(utf8_char_size_t-i-2)));
}
return utf8_char;
}
This should work either for 4, 3, 2 or 1 utf8 hex char.
Yes, and it can be made even more general. You need not restrict utf8hex
to contain the representation of only one code point. (If you're not
used to Unicode terminology, look e.g. at Wikipedia,
http://en.wikipedia.org/wiki/Unicode.)
char* utf8hex_to_str(const char* utf8hex)
{
const size_t utf8_hex_len = strlen(utf8hex);
if (utf8_hex_len & 1)
{
return 0; // Error. Odd number of hex digits.
}
const size_t utf8_char_len = utf8_hex_len/2;
char* utf8_char = new char[utf8_char_len+1];
for (size_t i = 0; i < utf8_char_len; i++)
{
int utf8int;
sscanf(utf8hex+2*i, "%2x", &utf8int);
utf8_char[i] = (char)utf8int;
}
utf8_char[utf8_char_len] = '\0';
return utf8_char;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]