u8string

以文本模式进行保存

#include <fstream>
#include <iostream>

int main() {
    std::u8string u8str = u8"你好𣍐,世界!This is a UTF-8 string.";
    const char* filename = "output_u8.txt";

    std::ofstream outfile(filename); // 默认以文本模式打开
    if (outfile.is_open()) {
        outfile << reinterpret_cast<const char*>(u8str.c_str());
        outfile.close();
        std::cout << "std::u8string 已写入文件 " << filename << " (UTF-8)." << std::endl;
    }
    else {
        std::cerr << "无法打开文件 " << filename << std::endl;
        return 1;
    }

    return 0;
}

以二进制模式进行保存

#include <fstream>
#include <iostream>

int main() {
    std::u8string u8str = u8"你好𣍐,世界!This is a UTF-8 string.";
    const char* filename = "output_u8.txt";

    std::ofstream outfile(filename, std::ios::binary); // 以二进制模式打开
    if (outfile.is_open()) {
        outfile.write(reinterpret_cast<const char*>(u8str.c_str()), u8str.length() * sizeof(char8_t));
        outfile.close();
        std::cout << "std::u8string 已写入文件 " << filename << " (UTF-8)." << std::endl;
    }
    else {
        std::cerr << "无法打开文件 " << filename << std::endl;
        return 1;
    }

    return 0;
}

u16string

以UTF-16 Little-Endian进行保存

#include <fstream>
#include <iostream>

int main() {
    std::u16string u16str = u"你好𣍐,世界!This is a test.";
    const char* filename = "output_u16le.txt";

    std::ofstream outfile(filename, std::ios::binary);
    if (outfile.is_open()) {
        // 写入 UTF-16 Little-Endian BOM
        unsigned char bom[] = { 0xFF, 0xFE };
        outfile.write(reinterpret_cast<const char*>(bom), sizeof(bom));

        // 将 std::u16string 的数据作为原始字节写入
        outfile.write(reinterpret_cast<const char*>(u16str.c_str()), u16str.length() * sizeof(char16_t));

        outfile.close();
        std::cout << "std::u16string 已写入文件 " << filename << " (UTF-16 Little-Endian)." << std::endl;
    }
    else {
        std::cerr << "无法打开文件 " << filename << std::endl;
        return 1;
    }

    return 0;
}

以UTF-16 Big-Endian进行保存

#include <fstream>
#include <iostream>
#include <vector>

int main() {
    std::u16string u16str = u"你好𣍐,世界!This is a test.";
    const char* filename = "output_u16be.txt";

    std::ofstream outfile(filename, std::ios::binary);
    if (outfile.is_open()) {
        // 写入 UTF-16 Big-Endian BOM
        unsigned char bom[] = { 0xFE, 0xFF };
        outfile.write(reinterpret_cast<const char*>(bom), sizeof(bom));

        // 将 std::u16string 的数据作为原始字节写入 (需要字节序转换)
        std::vector<char> buffer(u16str.length() * sizeof(char16_t));
        std::memcpy(buffer.data(), u16str.c_str(), buffer.size());

        // 交换每两个字节的顺序以实现 Big-Endian
        for (size_t i = 0; i < buffer.size(); i += 2) {
            std::swap(buffer[i], buffer[i + 1]);
        }

        outfile.write(buffer.data(), buffer.size());

        outfile.close();
        std::cout << "std::u16string 已写入文件 " << filename << " (UTF-16 Big-Endian)." << std::endl;
    }
    else {
        std::cerr << "无法打开文件 " << filename << std::endl;
        return 1;
    }

    return 0;
}
分类: 标签: 编码

评论

-- 评论已关闭 --

全部评论