ShowCnMsgOnDebian

    xiaoxiao2021-04-15  76

    前言

    帮同事做了一个小实验, 在xshell上显示中文, 并将gbk内容从文件中读出来转成uft8

    记录

    // @file testShowCnMsgOnDebian.cpp // @brief test show chinese msg on debian by xshell // 如果要显示工程内的固定中文字符串, 需要在makefile中,用iconv将工程中包含中文字符串的文件由gbk转成utf8编码 // 如果要将从外部文件或接口中传来的gbk字符串入库成utf8(mongodb中只存utf8), 需要用iconv(先iconv_open, 后iconv_close)将gbk转成utf8 // 如果要在控制台(e.g. xshell)显示中文, 需要在运行程序之前, 先运行 export LC_CTYPE=zh_CN.utf8 // 对于xshell, 即使debian有其他显示的编码支持, 也只有 export LC_CTYPE=zh_CN.utf8 好使. #include <stdlib.h> #include <stdio.h> #include <locale.h> // for setlocale #include <string.h> // for strlen #include <iconv.h> // for iconv // @ref http://www.cnblogs.com/hnrainll/archive/2011/05/07/2039700.html #define CSET_GBK "GBK" #define CSET_UTF8 "UTF-8" #define LC_NAME_zh_CN "zh_CN" #define LC_NAME_zh_CN_GB2312 LC_NAME_zh_CN "" #define LC_NAME_zh_CN_GBK LC_NAME_zh_CN "." CSET_GBK #define LC_NAME_zh_CN_UTF8 LC_NAME_zh_CN "." CSET_UTF8 #define LC_NAME_zh_CN_DEFAULT LC_NAME_zh_CN_GB2312 int testcase_read_and_show_file(); /* UNICODE to GB2312 */ int u2g(char* inbuf, size_t inlen, char* outbuf, size_t outlen); /* GB2312 to UNICODE */ int g2u(char* inbuf, size_t inlen, char* outbuf, size_t outlen); /* one charset to another */ int code_convert(const char* from_charset, const char* to_charset, char* inbuf, size_t inlen, char* outbuf, size_t outlen); // on first run console, input below, only change to zh_CN.utf8 // export LC_CTYPE=zh_CN.utf8 int main(int argc, char* argv[]) { char* pcLocale = NULL; // on debian, re config locales // dpkg-reconfigure locales /** zh_CN.UTF-8 UTF-8 zh_CN.GB18030 GB18030 zh_CN.GBK GBK zh_CN GB2312 zh_HK.UTF-8 UTF-8 zh_HK BIG5-HKSCS zh_SG.UTF-8 UTF-8 zh_SG.GBK GBK zh_SG GB2312 zh_TW.UTF-8 UTF-8 zh_TW.EUC-TW EUC-TW zh_TW BIG5 zu_ZA.UTF-8 UTF-8 zu_ZA ISO-8859-1 */ pcLocale = setlocale(LC_ALL, LC_NAME_zh_CN_DEFAULT); if (NULL != pcLocale) { printf("\n%s\n", pcLocale); } printf("中文测试1\n"); printf("============================================================\n"); printf(">> testcase build time 2017-4-13 14:33\n"); printf("============================================================\n"); testcase_read_and_show_file(); if (NULL != pcLocale) { pcLocale = setlocale(LC_ALL, pcLocale); printf("\n%s\n", pcLocale); } printf("============================================================\n"); printf("END, press any key to quit\n"); printf("============================================================\n"); getchar(); return 0; } int testcase_read_and_show_file() { FILE* fp; const char* pCfgFileName = "./file_for_test"; int i; char szBuf[1024] = {'\0'}; char szBufUtf8[1024] = {'\0'}; // printf("中文\n"); // wprintf("%ls/n", L"中文测试"); printf("============================================================\n"); printf("const char* on project : %s\n", "中文测试2.1"); // can printf ok // printf("%ls\n", "中文测试2.2"); // print empty // // wprintf(L"%s\n", L"中文测试2.3"); // print empty // wprintf(L"%ls\n", L"中文测试2.4"); // print empty // // wprintf(L"%s\n", "中文测试2.5"); // print empty // wprintf(L"%ls\n", "中文测试2.6"); // print empty printf("============================================================\n"); fp = fopen(pCfgFileName, "r"); printf("%s %s\n", (NULL != fp) ? "find" : "not find", pCfgFileName); if (fp != NULL) { if (NULL != fp) { printf("============================================================\n"); printf("const char* from file : \n"); printf("============================================================\n"); while (NULL != fgets(szBuf, sizeof(szBuf), fp)) { printf("print org : %s\n", szBuf); g2u(szBuf, strlen(szBuf), szBufUtf8, sizeof(szBufUtf8)); printf("print utf8 : %s\n\n", szBufUtf8); } } fclose(fp); } return 0; } int code_convert(const char* from_charset, const char* to_charset, char* inbuf, size_t inlen, char* outbuf, size_t outlen) { iconv_t cd; int rc; char** pin = &inbuf; char** pout = &outbuf; cd = iconv_open(to_charset, from_charset); if (cd == 0) { return -1; } memset(outbuf, 0, outlen); if (iconv(cd, pin, &inlen, pout, &outlen) == -1) { return -1; } iconv_close(cd); return 0; } int u2g(char* inbuf, size_t inlen, char* outbuf, size_t outlen) { return code_convert("utf-8", "gb2312", inbuf, inlen, outbuf, outlen); } int g2u(char* inbuf, size_t inlen, char* outbuf, size_t outlen) { return code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, outlen); } # testcase by lostspeed # if makefile edit on windows, use astyle format it to move \r\n to \n # first build do : chmod 777 * # build cmd is : ./Makefile clear rm ./testcase rm ./*.o # testShowCnMsgOnDebian.cpp iconv -f gb2312 -t utf8 testShowCnMsgOnDebian.cpp > testShowCnMsgOnDebian_utf8.cpp g++ testShowCnMsgOnDebian_utf8.cpp -o testcase \ -L/usr/local/lib \ -lpthread ls echo run ./testcase
    转载请注明原文地址: https://ju.6miu.com/read-671111.html

    最新回复(0)