Tuesday, May 21, 2013

NSString NSWindowsCP1251StringEncoding example ios


String Encodings - NSWindowsCP1251StringEncoding

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
NSWindowsCP1251StringEncoding
Microsoft Windows codepage 1251, encoding Cyrillic characters; equivalent to AdobeStandardCyrillic font encoding.
Discussion of [NSString NSWindowsCP1251StringEncoding]
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 NSWindowsCP1251StringEncoding]

NSString *myStr = [[[NSString alloc] initWithData:[response responseData] encoding:NSWindowsCP1251StringEncoding] autorelease];
    myStr = [myStr stringByReplacingOccurrencesOfString:@"encoding=\"windows-1251\"" withString:@""];
    myStr = [myStr stringByReplacingOccurrencesOfString:@"US-ASCII" withString:@"UTF-8"];
    NSData* aData = [myStr dataUsingEncoding:NSASCIIStringEncoding];

    rssParser = [[NSXMLParser alloc] initWithData:aData];
    [rssParser setDelegate:self];
    [rssParser setShouldProcessNamespaces:NO];
    [rssParser setShouldReportNamespacePrefixes:NO];
    [rssParser setShouldResolveExternalEntities:NO];

    [rssParser parse];

Example of [NSString NSWindowsCP1251StringEncoding]
// Convert it back to CP1251
NSData *dt = [mystr dataUsingEncoding:NSWindowsCP1251StringEncoding];

// Now load it as UTF8
NSString *str = [NSString alloc] initWithData:dt encoding:NSUTF8StringEncoding];
Example of [NSString NSWindowsCP1251StringEncoding]
NSString *textRev=[[NSString alloc]initWithData:data encoding:NSWindowsCP1251StringEncoding];

 NSLog(@" Actual String.. %@",textRev);