Skip to content

Instantly share code, notes, and snippets.

@scriptum
Last active December 22, 2015 15:58
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 scriptum/6495700 to your computer and use it in GitHub Desktop.
Save scriptum/6495700 to your computer and use it in GitHub Desktop.
This macros allows easily convert Utf-8 encoded string to array of UNICODE characters (unsigned int)
// a - your string (char *), i - iterator (uint), c - output character (uint)
#define UTF8_TO_UINT(a, i) \
if((a[i] & 0b10000000) == 0) { \
c = (unsigned)a[i++]; \
} \
else if (((unsigned)a[i] & 0b11100000) == 0b11000000) { \
c = ((unsigned)a[i++] & 0b00011111) << 6; \
c |= (unsigned)a[i++] & 0b00111111; \
} \
else if (((unsigned)a[i] & 0b11110000) == 0b11100000) { \
c = ((unsigned)a[i++] & 0b00001111) << 12; \
c |= ((unsigned)a[i++] & 0b00111111) << 6; \
c |= (unsigned)a[i++] & 0b00111111; \
} \
else if (((unsigned)a[i] & 0b11111000) == 0b11110000) { \
c = ((unsigned)a[i++] & 0b00000111) << 18; \
c |= ((unsigned)a[i++] & 0b00111111) << 12; \
c |= ((unsigned)a[i++] & 0b00111111) << 6; \
c |= (unsigned)a[i++] & 0b00111111; \
} else { /* Error! */ \
c = 0; \
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment