Skip to content

Instantly share code, notes, and snippets.

@devinacker
Last active Aug 29, 2015
Embed
What would you like to do?
#include <glib.h>
#include <stdio.h>
int main() {
// test a string with non-ASCII code points below U+0100
const unsigned char test1[] = "http://google.com/?q=tést";
// test a string with code points above U+0100
const unsigned char test2[] = "http://google.com/?q=テスト";
GError *err = NULL;
char *loc;
if (loc = g_locale_from_utf8(test1, -1, NULL, NULL, &err)) {
printf("test1: translated string is %s\n", loc);
g_free(loc);
} else if (err) {
printf("test1 error: %s\n", err->message);
g_error_free(err);
err = NULL;
}
printf("test2: UTF-8 byte sequence is: ");
for (int i = 0; test2[i]; i++)
printf("%02X ", test2[i]);
printf("\n");
if (loc = g_locale_from_utf8(test2, -1, NULL, NULL, &err)) {
printf("test2: translated string is %s\n", loc);
g_free(loc);
} else if (err) {
printf("test2 error: %s\n", err->message);
g_error_free(err);
err = NULL;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment