Skip to content

Instantly share code, notes, and snippets.

@devinacker
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devinacker/cd09eb2ab4608b3d90f1 to your computer and use it in GitHub Desktop.
Save devinacker/cd09eb2ab4608b3d90f1 to your computer and use it in GitHub Desktop.
#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