linux 下 gbk 转 utf8,

查看 40|回复 1
作者:亲爱的花开了   
// 大家看一下 ChatGPT 的代码
// Convert GBK encoded string to UTF-8 encoded string
int gbk2utf8(std::string &input) {
    iconv_t cd = iconv_open(“UTF-8“, “GBK“); // Create an iconv handle for encoding conversion
    if (cd == (iconv_t)(-1)) { // If iconv_open() call failed
        // std::cerr (input.c_str()); // Define input buffer pointer
    size_t inbytesleft = input.length(); // Define input buffer size
    static threadlocal outbuf char[1024 * 32]; // Define output buffer
    char* outptr = outbuf; // Define output buffer pointer
    size_t outbytesleft = sizeof(outbuf); // Define output buffer size
    while (inbytesleft > 0) {
        // Convert data in the input buffer to data in the output buffer
        size_t res = iconv(cd, &inbuf, &inbytesleft, &outptr, &outbytesleft);
        if (res == (size_t)(-1)) { // If iconv() call failed
            // std::cerr << “iconv() failed: “ << strerror(errno) << std::endl;
            iconv_close(cd); // Close the iconv handle
            return -1;
        }
    }
    input = std::string(outbuf, sizeof(outbuf) - outbytesleft); // Save the data in the output buffer to the output parameter “output“
    iconv_close(cd); // Close the iconv handle
    return 0;
}

看一下, 代码

妖友好好记   
您需要登录后才可以回帖 登录 | 立即注册

返回顶部