【问题标题】:How do I store the data within CFDataRef in unicode (BE) format?如何以 unicode (BE) 格式将数据存储在 CFDataRef 中?
【发布时间】:2012-09-26 16:04:02
【问题描述】:

我正在将字符串数据写入一个文件,其中数据应该以 ASCII 格式写入,然后基本上以 Unicode 格式重复。 ASCII 部分运行良好,但 Unicode 版本出现乱码。使用 CFStringCreateMutableWithExternalCharactersNoCopy 显式创建 Unicode 字符串的尝试失败了。建议和示例代码表示赞赏。

【问题讨论】:

    标签: objective-c cfstring cfdata


    【解决方案1】:

    例如,您可以使用CFStringCreateExternalRepresentationCFStringRef 转换为CFDataRef

    CFStringRef s = CFSTR("abc €");
    CFDataRef d = CFStringCreateExternalRepresentation(NULL, s, kCFStringEncodingUTF16BE, 0);
    // d now contains the bytes 00 61 00 62 00 63 00 20 20 ac.
    

    或者NSString/NSData:

    NSString *s = @"abc €";
    NSData *d = [d dataUsingEncoding:NSUTF16BigEndianStringEncoding];
    

    【讨论】:

      猜你喜欢
      • 2012-09-08
      • 1970-01-01
      • 1970-01-01
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      • 2013-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多