Skip to content

Instantly share code, notes, and snippets.

@fowlmouth
Last active March 18, 2022 17:30
Show Gist options
  • Save fowlmouth/92ec07934fd29197734142b17ecef66c to your computer and use it in GitHub Desktop.
Save fowlmouth/92ec07934fd29197734142b17ecef66c to your computer and use it in GitHub Desktop.
c unicode examples
#include <wchar.h>
#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
int main()
{
setlocale(LC_CTYPE, "");
FILE* f = fopen("test.string", "r");
long int position = ftell(f);
wint_t character = fgetwc(f);
while(character != WEOF)
{
printf("char: %lc position: %ld\n", character, position);
position = ftell(f);
character = fgetwc(f);
}
fclose(f);
return 0;
}
ALL: file string
string: string.o
cc -o $@ $<
file: file.o
cc -o $@ $<
#include <wchar.h>
#include <stdio.h>
#include <locale.h>
#include <string.h>
#include <stdlib.h>
int main()
{
setlocale(LC_CTYPE, "");
const char *test_string = "A🍄B🌶C🍍";
const char *test_string_end = test_string + strlen(test_string);
mbstate_t mbs;
mbsinit(&mbs);
for(const char* str = test_string; str < test_string_end; )
{
wchar_t wchar;
const int mbresult = mbtowc(&wchar, str, test_string_end - str);
printf("mbtowc result: %d char: %lc\n", mbresult, wchar);
if(mbresult <= 0)
break;
str += mbresult;
}
return 0;
}
A🍄B🌶C🍍
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment