How can I make Unicode characters integers?

I want to create an array of Unicode characters, but I don't know how to convert integers to a Unicode representation. Here is the code that I still have

NSMutableArray *uniArray = [[NSMutableArray alloc] initWithCapacity:0];
int i;

for (i = 32; i < 300; i++) {
    NSString *uniString = [NSString stringWithFormat:@"\u%04X", i];
    [uniArray addObject:uniString];
}

Which gives me the error "incomplete universal symbol name \ u"

Is there a better way to create an array of Unicode characters? Thanks.

+5
source share
4 answers

You must use% C to insert a unicode character:

NSMutableArray *uniArray = [[NSMutableArray alloc] initWithCapacity:0];
int i;

for (i = 32; i < 300; i++) {
   NSString *uniString = [NSString stringWithFormat:@"%C", i];
   [uniArray addObject:uniString];
}

Another way (better?) Uses stringWithCharacters:

NSMutableArray *uniArray = [[NSMutableArray alloc] initWithCapacity:0];
int i;

for (i = 32; i < 300; i++) {
   NSString *uniString = [NSString stringWithCharacters:(unichar *)&i length:1];
   [uniArray addObject:uniString];
}
+7
source

, \u . "%04x", -, , - , - , .

+2

UTF-16, [NSString stringWithCharacters:&character length:1]. UTF-32, -initWithData:encoding:, , ( , UTF-32, ).

+1

:

RegexKitLite required . Uses a regular expression (?s).to split a string of Unicode characters into NSArray. The regex operator .by default matches all but newline characters, and the sequence (?s)says Turn on the Dot All regex option, which .also allows you to match the newline character. This is important, as we obviously go through at least \nthe example below.

#import <Foundation/Foundation.h>
#import "RegexKitLite.h"

// Compile with: gcc -std=gnu99 -o unicodeArray unicodeArray.m RegexKitLite.m -framework Foundation -licucore

int main(int argc, char *argv[]) {
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  unichar uc[1024];
  for(NSUInteger idx = 0UL; idx < 1024UL; idx++) { uc[idx] = (unichar)idx; }
  NSArray *unicharArray = [[NSString stringWithCharacters:uc length:1024UL] componentsMatchedByRegex:@"(?s)."];

  NSLog(@"array: %@", [unicharArray subarrayWithRange:NSMakeRange(32UL, (1024UL - 32UL))]);

  [pool release];
  return(0);
}
0
source

All Articles