C ++ programming Chinese converted to Unicode

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/wangduanqiugao/article/details/102741544

Xie _ monopoly * high autumn, using VS2019 written in Chinese converted to Unicode, namely: ChineseToUnicode

Input: monopoly early autumn snow level to be high and

Output: \ u671b \ u65ad \ u79cb \ u9ad8 \ u548c \ u5f85 \ u96ea \ u521d \ u5e73

C ++ console project: conversion of Unicode and Chinese - Download:

https://download.csdn.net/download/wangduanqiugao/11924338

C ++ console projects in VS2019 (x64 \ x86 \ Debug \ Release compiled)

#include <atlstr.h>
#include <string.h>
#include <iostream>
using namespace std;

bool ChineseToUnicode(CString cstr, string & str)
{
    int i = 0;
    int strlen = 0;
    int hexlen = 0;
    long hexlong = 0;

    strlen = cstr.GetLength();
    if (strlen <= 0)
    {
        return false;
    }

    wchar_t* wchs = new wchar_t[strlen + 1];
    memset(wchs, 0, sizeof(wchar_t) * (strlen + 1));
    wcscpy_s(wchs, strlen + 1, cstr.GetBuffer(strlen));

    hexlen = strlen * 7;
    char* hexstr = new char[hexlen + 1];
    memset(hexstr, 0, hexlen + 1);

    char tchar[7];
    wchar_t* szHex = wchs;

    for (i = 0; i < strlen; i++)
    {
	hexlong = (long)(*szHex++);
	sprintf_s(tchar, "\\u%04x", hexlong);
	strcat_s(hexstr, hexlen, tchar);
    }

    str = (string)hexstr;

    if (wchs)
    {
	delete[] wchs;
    }
    if (hexstr)
    {
	delete[] hexstr;
    }
    return true;
}

int main()
{
    CString m_Chinese = _T("望断秋高和待雪初平");
    string  m_ChineseToUnicode;

    //ChineseToUnicode
    ChineseToUnicode(m_Chinese, m_ChineseToUnicode);

    wcout.imbue(locale("chs"));
    wcout << m_Chinese.GetString() << endl;
    cout << "ChineseToUnicode:" << "\n" << m_ChineseToUnicode << "\n\n\n";
    getchar();
}

 

Guess you like

Origin blog.csdn.net/wangduanqiugao/article/details/102741544