Tuesday, May 21, 2013

NSString NSUTF32StringEncoding example ios


String Encodings - NSUTF32StringEncoding

The following constants are provided by NSString as possible string encodings.
enum {
   NSASCIIStringEncoding = 1,
   NSNEXTSTEPStringEncoding = 2,
   NSJapaneseEUCStringEncoding = 3,
   NSUTF8StringEncoding = 4,
   NSISOLatin1StringEncoding = 5,
   NSSymbolStringEncoding = 6,
   NSNonLossyASCIIStringEncoding = 7,
   NSShiftJISStringEncoding = 8,
   NSISOLatin2StringEncoding = 9,
   NSUnicodeStringEncoding = 10,
   NSWindowsCP1251StringEncoding = 11,
   NSWindowsCP1252StringEncoding = 12,
   NSWindowsCP1253StringEncoding = 13,
   NSWindowsCP1254StringEncoding = 14,
   NSWindowsCP1250StringEncoding = 15,
   NSISO2022JPStringEncoding = 21,
   NSMacOSRomanStringEncoding = 30,
   NSUTF16StringEncoding = NSUnicodeStringEncoding,
   NSUTF16BigEndianStringEncoding = 0x90000100,
   NSUTF16LittleEndianStringEncoding = 0x94000100,
   NSUTF32StringEncoding = 0x8c000100,
   NSUTF32BigEndianStringEncoding = 0x98000100,
   NSUTF32LittleEndianStringEncoding = 0x9c000100,
   NSProprietaryStringEncoding = 65536
};
Constants
NSUTF32StringEncoding
32-bit UTF encoding.
Discussion of [NSString NSUTF32StringEncoding]
These values represent the various character encodings supported by the NSStringclasses. This is an incomplete list. Additional encodings are defined in String Programming Guide for Core Foundation (see CFStringEncodingExt.h); these encodings can be used with NSString by first passing the Core Foundation encoding to the CFStringConvertEncodingToNSStringEncoding function.


Example of [NSString NSUTF32StringEncoding]

NSString * transformedString = [[NSString alloc]
    initWithBytes: sourceString.c_str()
    length:        sourceString.size() * sizeof(wchar_t)
    encoding:      NSUTF32StringEncoding];

Example of [NSString NSUTF32StringEncoding]
NSScanner *scan = [[NSScanner alloc] initWithString:@"1F61E"];
unsigned int val;
[scan scanHexInt:&val];
char cc[4];
cc[3] = (val >> 0) & 0xFF;
cc[2] = (val >> 8) & 0xFF;
cc[1] = (val >> 16) & 0xFF;
cc[0] = (val >> 24) & 0xFF;
NSString *s = [[NSString alloc]
    initWithBytes:cc
           length:4
         encoding:NSUTF32StringEncoding];
NSLog(@"[%@]", s);
Example of [NSString NSUTF32StringEncoding]
NSString *myAccentStr = @"José";
NSUInteger l1 = [myAccentStr lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
NSUInteger l2 = [myAccentStr lengthOfBytesUsingEncoding:NSUTF16StringEncoding];
NSUInteger l3 = [myAccentStr lengthOfBytesUsingEncoding:NSUTF32StringEncoding];
NSLog(@"%ld %ld %ld", (long)l1, (long)l2, (long)l3);

> 5, 8, 16