In-depth parsing of coding conversion methods in IOS development

Source: Internet
Author: User

IOS developmentMediumEncoding conversionThe method is the content to be introduced in this article, mainly to learnEncoding conversionFor more information, see the details. You can use the following methodsEncoding conversion.

 
 
  1. NSString* str2 = [NSString stringWithCString:str1 encoding:enc1]; 

Call the cStringUsingEncoding: enc2 of NSString to convert from enc1 to enc2.

However, it is difficult to obtain the enc enumerated values.

1. Only the following encoding values are defined in NSString. h.

 
 
  1. enum {  
  2.     NSASCIIStringEncoding = 1,                 
  3.     NSNEXTSTEPStringEncoding = 2,  
  4.     NSJapaneseEUCStringEncoding = 3,  
  5.     NSUTF8StringEncoding = 4,  
  6.     NSISOLatin1StringEncoding = 5,  
  7.     NSSymbolStringEncoding = 6,  
  8.     NSNonLossyASCIIStringEncoding = 7,  
  9.     NSShiftJISStringEncoding = 8,           
  10.     NSISOLatin2StringEncoding = 9,  
  11.     NSUnicodeStringEncoding = 10,  
  12.     NSWindowsCP1251StringEncoding = 11,     
  13.     NSWindowsCP1252StringEncoding = 12,     
  14.     NSWindowsCP1253StringEncoding = 13,     
  15.     NSWindowsCP1254StringEncoding = 14,     
  16.     NSWindowsCP1250StringEncoding = 15,     
  17.     NSISO2022JPStringEncoding = 21,          
  18.     NSMacOSRomanStringEncoding = 30,  
  19.  
  20.     NSUTF16StringEncoding = NSUnicodeStringEncoding,        
  21.  
  22. #if MAC_OS_X_VERSION_10_4 <= MAC_OS_X_VERSION_MAX_ALLOWED || __IPHONE_2_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED  
  23.     NSUTF16BigEndianStringEncoding = 0x90000100,           
  24.     NSUTF16LittleEndianStringEncoding = 0x94000100,        
  25.  
  26.     NSUTF32StringEncoding = 0x8c000100,                     
  27.     NSUTF32BigEndianStringEncoding = 0x98000100,           
  28.     NSUTF32LittleEndianStringEncoding = 0x9c000100          
  29. #endif  
  30. }; 

Is Chinese in iphone not supported yet? No, right?

2. The iphone must support Chinese characters, but this enumeration value is not defined in NSString. We can obtain this enumeration value in two ways. The first method is CFStringConvertEncodingToNSStringEncoding.
For example:

 
 
  1. NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);  
  2. NSString* str2 = [NSString stringWithCString:str1 encoding:enc]; 

For all kCFStringEncoding definitions, see CFStringEncodingExt. h. CFStringConvertIANACharSetNameToEncoding is also useful.

3. Another method is

 
 
  1. const NSStringEncoding *encodings = [NSString availableStringEncodings];  
  2. NSMutableString *str = [[NSMutableString alloc] init];  
  3. NSStringEncoding encoding;  
  4. while ((encoding = *encodings++) != 0)  
  5. {  
  6.          [str appendFormat: @"%@ === %in", [NSString localizedNameOfStringEncoding:encoding], encoding];  

Print the str array, and you can know the enumerated value of each encoding, for example,-2147482063 in Chinese.

Summary: in-depth analysisIOS developmentMediumEncoding conversionThe content of this method has been introduced. I hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.