ios 端如何 进行同样的unicode 解码 转码, 不太懂Java,希望大神贴上ios unicode代码转码解码
public static String string2Unicode(String string) {
StringBuffer unicode = new StringBuffer();
for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
unicode.append("\\u" + Integer.toHexString(c));
}
String str = unicode.toString();
return str.replaceAll("\\\\","0x");
}
public static String unicode2String(String unicode) {
String str = unicode.replace("0x", "\\");
StringBuffer string = new StringBuffer();
String[] hex = str.split("\\\\u");
for (int i = 1; i < hex.length; i++) {
int data = Integer.parseInt(hex[i], 16);
string.append((char) data);
}
return string.toString();
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
如果你是在用 obj c 的话,你可以看看 NSString 的文档,大概就是下面两个方法, string 的编码和解码:
(NSString )decodeWithUnicode:(NSString )description;
{
description = [description stringByReplacingOccurrencesOfString:@"0x" withString:@"\\"];
NSString *tempstr = [NSString string];
NSArray *studyy =[description componentsSeparatedByString:@"\\u"];
for (NSString *temp in studyy) {
}
tempstr = [tempstr stringByReplacingOccurrencesOfString:@" " withString:@""];
NSMutableData *commandToSend= [[NSMutableData alloc] init];
unsigned char whole_byte;
char byte_chars[3] = {'0','0','0'};
int i;
for (i=0; i < [tempstr length]/2; i++) {
}
NSString *unicodeDes = [[NSString alloc]initWithData:commandToSend encoding:(NSUTF16StringEncoding)];
return unicodeDes;
}
@end