Skip to content

Instantly share code, notes, and snippets.

@fjolnir
Created April 21, 2015 09:08
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 fjolnir/5aeee451ca3456a44726 to your computer and use it in GitHub Desktop.
Save fjolnir/5aeee451ca3456a44726 to your computer and use it in GitHub Desktop.
// Ref: http://clang.llvm.org/doxygen/ConvertUTF_8c_source.html
#import <Foundation/Foundation.h>
static uint8_t const firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
static uint32_t const byteMark = 0x80;
static uint32_t const byteMask = 0xBF;
int main(int argc, char *argv[]) {
unsigned int input = strtol("0001F30D", NULL, 16);
size_t const len = input < 0x80 ? 1
: input < 0x800 ? 2
: input < 0x10000 ? 3
: 4;
uint8_t bytes[len];
uint8_t *str = bytes+len;
switch(len) {
case 4: *--str = (uint8_t)((input | byteMark) & byteMask); input >>= 6;
case 3: *--str = (uint8_t)((input | byteMark) & byteMask); input >>= 6;
case 2: *--str = (uint8_t)((input | byteMark) & byteMask); input >>= 6;
case 1: *--str = (uint8_t) (input | firstByteMark[len]);
}
NSLog(@"'%@'", [[NSString alloc] initWithBytes:(char*)str length:len encoding:NSUTF8StringEncoding]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment