将"wstring"转换为"const UInt8 *"

Convert "wstring" to "const UInt8 *"

本文关键字:UInt8 const 转换 wstring      更新时间:2023-10-16

我不得不将wstring转换为(UInt8*)。另外,我有一个字符串:

    wstring str;

你能帮我皈依吗?我试过这个代码,但它不起作用:

    wstring str;
    uint8 *buf = reinterpret_cast<uint8>(str);
    CFStringRef CFStringCreateWithBytes (CFAllocatorRef alloc,
                                         const UInt8 *buf,
                                         CFIndex sizeOfBuf,
                                         CFStringEncoding encoding,
                                         Boolean isExternalRepresentation);

wchar_t在OSX上是32位的,因此std::wstring可以保存一个UTF-32编码的字符串:

std::wstring str = ...;
CFStringRef encoded = CFStringCreateWithBytes(
                                     kCFAllocatorDefault,
                                     reinterpret_cast<uint8*>(str.c_str()),
                                     str.size() * sizeof(wchar_t),
                                     kCFStringEncodingUTF32LE,
                                     false);