Skip to content

Instantly share code, notes, and snippets.

@mfukar
Last active December 16, 2015 10:28
Show Gist options
  • Save mfukar/5419950 to your computer and use it in GitHub Desktop.
Save mfukar/5419950 to your computer and use it in GitHub Desktop.
Epic Fail UCS-2 to UTF-8 conversion.
/* This is how a PhD holder thinks UCS-2 is converted to UTF-8 */
size_t gsm_convert_ucs2(const uint8_t *buf, size_t length, unsigned char *dest)
{
size_t i;
size_t j;
for (i=0,j=0;i<length;i+=2) {
if (buf[i] == 0)
dest[j++] = buf[i+1];
else {
snprintf((char *)&dest[j], 256-j, "\\%02x%02x", buf[i], buf[i+1]);
j+=5;
}
return j;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment