使用Objective C/Cocoa来取消unicode字符,即\ u1234

cor*_*ras 34 unicode cocoa objective-c

我从中获取数据的一些站点返回UTF-8字符串,其中UTF-8字符被转义,即: \u5404\u500b\u90fd

是否有内置的可可功能可能有助于此,或者我必须编写自己的解码算法.

Nik*_*uhe 91

Cocoa没有提供解决方案是正确的,但Core Foundation确实如此:CFStringTransform.

CFStringTransform生活在Mac OS(和iOS)的一个尘土飞扬,偏远的角落,所以这是一个有点知道的宝石.它是Apple的ICU兼容字符串转换引擎的前端.它可以执行真正的魔术,如希腊语和拉丁语(或任何已知的脚本)之间的音译,但它也可以用来做一些平凡的任务,比如从糟糕的服务器中取出字符串:

NSString *input = @"\\u5404\\u500b\\u90fd";
NSString *convertedString = [input mutableCopy];

CFStringRef transform = CFSTR("Any-Hex/Java");
CFStringTransform((__bridge CFMutableStringRef)convertedString, NULL, transform, YES);

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

// prints: ???, tada!
Run Code Online (Sandbox Code Playgroud)

正如我所说,CFStringTransform真的很强大.它支持许多预定义的转换,例如大小写映射,规范化或unicode字符名称转换.您甚至可以设计自己的转换.

我不知道为什么Apple不能从Cocoa中获取它.

编辑2015:

OS X 10.11和iOS 9将以下方法添加到Foundation:

- (nullable NSString *)stringByApplyingTransform:(NSString *)transform reverse:(BOOL)reverse;
Run Code Online (Sandbox Code Playgroud)

所以上面的例子变成......

NSString *input = @"\\u5404\\u500b\\u90fd";
NSString *convertedString = [input stringByApplyingTransform:@"Any-Hex/Java"
                                                     reverse:YES];

NSLog(@"convertedString: %@", convertedString);
Run Code Online (Sandbox Code Playgroud)

谢谢@nschmidt的提醒.

  • +1,这是一个更简单的答案. (3认同)
  • @shiami见http://userguide.icu-project.org/transforms/general#TOC-ICU-Transliterators (2认同)

ken*_*ytm 24

没有内置函数可以进行C unescaping.

你可以欺骗一点,NSPropertyListSerialization因为"旧文本样式"plist支持C转义通过\Uxxxx:

NSString* input = @"ab\"cA\"BC\\u2345\\u0123";

// will cause trouble if you have "abc\\\\uvw"
NSString* esc1 = [input stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
NSString* esc2 = [esc1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
NSString* quoted = [[@"\"" stringByAppendingString:esc2] stringByAppendingString:@"\""];
NSData* data = [quoted dataUsingEncoding:NSUTF8StringEncoding];
NSString* unesc = [NSPropertyListSerialization propertyListFromData:data
                   mutabilityOption:NSPropertyListImmutable format:NULL
                   errorDescription:NULL];
assert([unesc isKindOfClass:[NSString class]]);
NSLog(@"Output = %@", unesc);
Run Code Online (Sandbox Code Playgroud)

但请注意,这不是很有效.如果你编写自己的解析器会好得多.(顺便说一句,您是在解码JSON字符串吗?如果是,您可以使用现有的JSON解析器.)


小智 12

这是我最后写的.希望这会帮助一些人.

+ (NSString*) unescapeUnicodeString:(NSString*)string
{
// unescape quotes and backwards slash
NSString* unescapedString = [string stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""];
unescapedString = [unescapedString stringByReplacingOccurrencesOfString:@"\\\\" withString:@"\\"];

// tokenize based on unicode escape char
NSMutableString* tokenizedString = [NSMutableString string];
NSScanner* scanner = [NSScanner scannerWithString:unescapedString];
while ([scanner isAtEnd] == NO)
{
    // read up to the first unicode marker
    // if a string has been scanned, it's a token
    // and should be appended to the tokenized string
    NSString* token = @"";
    [scanner scanUpToString:@"\\u" intoString:&token];
    if (token != nil && token.length > 0)
    {
        [tokenizedString appendString:token];
        continue;
    }

    // skip two characters to get past the marker
    // check if the range of unicode characters is
    // beyond the end of the string (could be malformed)
    // and if it is, move the scanner to the end
    // and skip this token
    NSUInteger location = [scanner scanLocation];
    NSInteger extra = scanner.string.length - location - 4 - 2;
    if (extra < 0)
    {
        NSRange range = {location, -extra};
        [tokenizedString appendString:[scanner.string substringWithRange:range]];
        [scanner setScanLocation:location - extra];
        continue;
    }

    // move the location pas the unicode marker
    // then read in the next 4 characters
    location += 2;
    NSRange range = {location, 4};
    token = [scanner.string substringWithRange:range];
    unichar codeValue = (unichar) strtol([token UTF8String], NULL, 16);
    [tokenizedString appendString:[NSString stringWithFormat:@"%C", codeValue]];

    // move the scanner past the 4 characters
    // then keep scanning
    location += 4;
    [scanner setScanLocation:location];
}

// done
return tokenizedString;
}

+ (NSString*) escapeUnicodeString:(NSString*)string
{
// lastly escaped quotes and back slash
// note that the backslash has to be escaped before the quote
// otherwise it will end up with an extra backslash
NSString* escapedString = [string stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
escapedString = [escapedString stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];

// convert to encoded unicode
// do this by getting the data for the string
// in UTF16 little endian (for network byte order)
NSData* data = [escapedString dataUsingEncoding:NSUTF16LittleEndianStringEncoding allowLossyConversion:YES];
size_t bytesRead = 0;
const char* bytes = data.bytes;
NSMutableString* encodedString = [NSMutableString string];

// loop through the byte array
// read two bytes at a time, if the bytes
// are above a certain value they are unicode
// otherwise the bytes are ASCII characters
// the %C format will write the character value of bytes
while (bytesRead < data.length)
{
    uint16_t code = *((uint16_t*) &bytes[bytesRead]);
    if (code > 0x007E)
    {
        [encodedString appendFormat:@"\\u%04X", code];
    }
    else
    {
        [encodedString appendFormat:@"%C", code];
    }
    bytesRead += sizeof(uint16_t);
}

// done
return encodedString;
}
Run Code Online (Sandbox Code Playgroud)