C++ GET UTF-8网页编码转换

string UTF8ToGBK(const std::string& strUTF8)                                //GBKתUTF-8
{
int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);
unsigned short * wszGBK = new unsigned short[len + 1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8.c_str(), -1, (LPWSTR)wszGBK, len);

len = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK = new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte(CP_ACP,0, (LPWSTR)wszGBK, -1, szGBK, len, NULL, NULL);
//strUTF8 = szGBK;
std::string strTemp(szGBK);
delete[]szGBK;
delete[]wszGBK;
return strTemp;
}

 

转载于:https://my.oschina.net/Thekillersohot/blog/699580

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是将GBK编码转换UTF-8编码的 C 语言函数: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> void GBK_to_UTF8(const char* gbk_filename, const char* utf8_filename) { FILE* gbk_file = fopen(gbk_filename, "rb"); if (!gbk_file) { printf("Error: Failed to open file '%s'.\n", gbk_filename); return; } // Get the file size. fseek(gbk_file, 0, SEEK_END); size_t gbk_size = ftell(gbk_file); fseek(gbk_file, 0, SEEK_SET); // Allocate memory for the file content. char* gbk_content = calloc(gbk_size + 1, sizeof(char)); if (!gbk_content) { printf("Error: Failed to allocate memory.\n"); fclose(gbk_file); return; } // Read the file content. size_t bytes_read = fread(gbk_content, sizeof(char), gbk_size, gbk_file); if (bytes_read != gbk_size) { printf("Error: Failed to read file '%s'.\n", gbk_filename); free(gbk_content); fclose(gbk_file); return; } // Close the file. fclose(gbk_file); // Convert the content from GBK to UTF-8. const char* gbk_encoding = "GBK"; const char* utf8_encoding = "UTF-8"; size_t utf8_size = iconv(gbk_encoding, utf8_encoding, gbk_content, gbk_size, NULL, 0); char* utf8_content = calloc(utf8_size + 1, sizeof(char)); if (!utf8_content) { printf("Error: Failed to allocate memory.\n"); free(gbk_content); return; } char* inbuf = gbk_content; char* outbuf = utf8_content; size_t inbytesleft = gbk_size; size_t outbytesleft = utf8_size; iconv_t conv = iconv_open(utf8_encoding, gbk_encoding); if (conv == (iconv_t)-1) { printf("Error: Failed to initialize iconv.\n"); free(utf8_content); free(gbk_content); return; } size_t result = iconv(conv, &inbuf, &inbytesleft, &outbuf, &outbytesleft); if (result == (size_t)-1) { printf("Error: Failed to convert encoding.\n"); free(utf8_content); free(gbk_content); iconv_close(conv); return; } // Close the iconv conversion context. iconv_close(conv); // Write the UTF-8 content to the file. FILE* utf8_file = fopen(utf8_filename, "wb"); fwrite(utf8_content, sizeof(char), utf8_size, utf8_file); fclose(utf8_file); // Free the memory. free(utf8_content); free(gbk_content); printf("Successfully converted file '%s' from GBK to UTF-8.\n", gbk_filename); } ``` 调用方式: ```c GBK_to_UTF8("input.txt", "output.txt"); ``` 以上函数将把`input.txt`文件(假设它是用GBK编码)的内容转换UTF-8编码,并将转换后的内容写入`output.txt`文件。如需要转换其他编码,可在相应参数更改编码名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值