ios字符转换

  • Unicode转化为汉字

+ (NSString *)replaceUnicode:(NSString *)unicodeStr {  

    

   NSString *tempStr1 = [unicodeStrstringByReplacingOccurrencesOfString:@"\\u"withString:@"\\U"];  

   NSString *tempStr2 = [tempStr1stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""];  

   NSString *tempStr3 = [[@"\""stringByAppendingString:tempStr2]stringByAppendingString:@"\""];  

   NSData *tempData = [tempStr3dataUsingEncoding:NSUTF8StringEncoding];  

   NSString* returnStr = [NSPropertyListSerializationpropertyListFromData:tempData  

                                                          mutabilityOption:NSPropertyListImmutable   

                                                                    format:NULL  

                                                          errorDescription:NULL];  

    

   return [returnStrstringByReplacingOccurrencesOfString:@"\\r\\n"withString:@"\n"];  

    

}


  • 汉字与utf8相互转化

 NSString* strA = [@"%E4%B8%AD%E5%9B%BD"stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

 NSString *strB = [@"中国"stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


  • NSString 转化为utf8

 NSString *strings = [NSStringstringWithFormat:@"abc"];

    

    NSLog(@"strings : %@",strings);

    

   CF_EXPORT

    CFStringRef CFURLCreateStringByAddingPercentEscapes(CFAllocatorRef allocator,CFStringReforiginalString,CFStringRef charactersToLeaved, CFStringReflegalURLCharactersToBeEscaped,CFStringEncoding encoding);

    

    NSString *encodedValue = (__bridge NSString*)CFURLCreateStringByAddingPercentEscapes(nil,                                    (__bridgeCFStringRef)strings,nil, (CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8);





  • 编码转换

  NSString *str = [@"&# 20918;&# 24314;&# 20844;&# 21496;&# 37325;&# 35270;&# 20892;&# 27665;&# 24037;&# 30340;&# 25945;&# 32946;&#  31649;&# 29702;" 

                    stringByReplacingOccurrencesOfString:@"&#"withString:@""];

   NSArray *arr = [strcomponentsSeparatedByString:@";"];

    for(NSString *v in arr){

        char *c = malloc(2);

        int value = [v intValue];

        c[1] = value  &0x00FF;

        c[0] = value >>8 &0x00FF;

       NSLog(@"%@",[NSStringstringWithCString:cencoding:NSUnicodeStringEncoding]);

        free(c);

    }



Q: Is there a standard method to package a Unicode character so it fits an 8-Bit ASCII stream?

A: There are three or four options for making Unicode fit into an 8-bit format.

a) Use UTF-8. This preserves ASCII, but not Latin-1, because the characters >127 are different from Latin-1. UTF-8 uses the bytes in the ASCII only for ASCII characters. Therefore, it works well in any environment where ASCII characters have a significance as syntax characters, e.g. file name syntaxes, markup languages, etc., but where the all other characters may use arbitrary bytes. 
Example: “Latin Small Letter s with Acute” (015B) would be encoded as two bytes: C5 9B.

b) Use Java or C style escapes, of the form \uXXXXX or \xXXXXX. This format is not standard for text files, but well defined in the framework of the languages in question, primarily for source files.
Example: The Polish word “wyjście” with character “Latin Small Letter s with Acute” (015B) in the middle (ś is one character) would look like: “wyj\u015Bcie".

c) Use the &#xXXXX; or &#DDDDD; numeric character escapes as in HTML or XML. Again, these are not standard for plain text files, but well defined within the framework of these markup languages.
Example: “wyjście” would look like “wyjście"

d) Use SCSU. This format compresses Unicode into 8-bit format, preserving most of ASCII, but using some of the control codes as commands for the decoder. However, while ASCII text will look like ASCII text after being encoded in SCSU, other characters may occasionally be encoded with the same byte values, making SCSU unsuitable for 8-bit channels that blindly interpret any of the bytes as ASCII characters.
Example: “<SC2> wyjÛcie” where <SC2> indicates the byte 0x12 and “Û” corresponds to byte 0xDB. [AF] & [KW]


如c所描述,这是一种“未标准"但广泛采用的做法,说是山寨编码也行 :-)

所以编码过程是

字符串 -> Unicode编码 -> &#xXXXX; or &#DDDDD; 

解码过程反过来即可 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 中使用 RSA 加密和解密需要使用到公钥和私钥,其中公钥可以转换字符串形式。具体步骤如下: 1. 导入 Security.framework 框架 2. 创建 SecKeyRef 对象 ```objc - (SecKeyRef)getPublicKey { NSString *publicKeyString = @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcNwJztMj+UyIeHJv6X9F6n/2/2jD6UxvTmJwY8C3q1zDfjTJbX9eW1aLlZa+RfCv5GJjW8VgJdMfR7Vv0X2O4tj3qJ4E4tVvzj+Kg8VQaQ2bPL7Jv5X5b5g5Z5hGp5x5YgldfM6Jy1e+HJGcE0WgDvJIDBj6UO2L0ZwY8D2Q0z0TJr+Iqz1W8lGcC9EwwIDAQAB"; NSData *data = [[NSData alloc] initWithBase64EncodedString:publicKeyString options:NSDataBase64DecodingIgnoreUnknownCharacters]; if (!data) { return nil; } NSString *tag = @"com.example.publickey"; NSMutableDictionary *attributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys: (__bridge id)kSecClassKey, (__bridge id)kSecClass, (__bridge id)kSecAttrKeyTypeRSA, (__bridge id)kSecAttrKeyType, tag, (__bridge id)kSecAttrApplicationTag, (__bridge id)kSecAttrKeyClassPublic, (__bridge id)kSecAttrKeyClass, nil]; SecKeyRef keyRef = SecKeyCreateWithData((__bridge CFDataRef)data, (__bridge CFDictionaryRef)attributes, nil); return keyRef; } ``` 3. 将 SecKeyRef 对象转换为公钥字符串 ```objc - (NSString *)getPublicKeyString { SecKeyRef keyRef = [self getPublicKey]; if (!keyRef) { return nil; } NSData *data = [self dataFromKey:keyRef]; NSString *base64String = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; return base64String; } - (NSData *)dataFromKey:(SecKeyRef)key { CFDataRef dataRef = SecKeyCopyExternalRepresentation(key, nil); if (!dataRef) { return nil; } NSData *data = (__bridge NSData *)dataRef; CFRelease(dataRef); return data; } ``` 以上代码将公钥字符转换为 SecKeyRef 对象,然后再将 SecKeyRef 对象转换为 NSData 对象,最终使用 base64 编码将 NSData 对象转换字符串形式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值