Question

I'm trying to figure out how to loop through and create an NSString from unicode values. I've figured out how to do it with the shorter codes:

    unichar buffer[26];
    unichar letter = 0x0041;
    for (int i = 0; i < 26; i++) {
        buffer[i] = letter;
        letter++;
    }
    NSLog(@"%@", [NSString stringWithCharacters: buffer length:26]);

That will get the alphabet. I also got how to do the longer codes:

    NSString *A = @"\U0001F30A";
    NSString *B = @"\U0001F30B";
    NSString *C = @"\U0001F30C";
    NSLog(@"Emoji: %@ %@ %@", A, B, C);

That gets 3 emojis.

What I'd like to be able to do is get a string of all unicode characters between \U00011111 and \U00022222 (random example). This is the first time I'm dealing with unicodes and this is completely stumping me. Any code snippets or pointers would be much appreciated.

Was it helpful?

Solution

Not all values between U+11111 and U+22222 are defined in the Unicode standard (compare http://www.unicode.org/Public/6.2.0/ucd/UnicodeData.txt), therefore I take a different range as an example:

uint32_t first = 0x1F300;
uint32_t last  = 0x1F310;
NSMutableString *string = [NSMutableString string];
for (uint32_t unicode = first; unicode <= last; unicode++) {
    // Create a string from the (4 byte, little-endian) unicode value:
    NSString *tmp = [[NSString alloc] initWithBytes:&unicode length:4 encoding:NSUTF32LittleEndianStringEncoding];

    [string appendString:tmp];
}
NSLog(@"%@", string);

Output: 🌀🌁🌂🌃🌄🌅🌆🌇🌈🌉🌊🌋🌌🌍🌎🌏🌐

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top