Created
April 21, 2015 09:08
-
-
Save fjolnir/5aeee451ca3456a44726 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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