1 Unicode编码下的全角字符转半角字符

如果输入的待转换的字符串是std::wstring型,那么直接对std::wstring中的字符进行遍历,将其中的全角字符转换为半角字符,具体的转换代码如下:

#include <iostream>
#include <locale>
#include <codecvt>

static std::wstring DoubleByteCharToSingleByteChar_Unicode(const std::wstring& srcStr)
{
    std::wstring dstStr = L"";

    int tempChar;
    int length = srcStr.length();

    for (int i = 0; i < length; i++)
    {
        tempChar = srcStr[i];
        if (tempChar == 12288)
        {
            tempChar = 32;
        }
        else if (tempChar >= 65281 && tempChar <= 65374)
        {
            tempChar -= 65248;
        }

        dstStr += tempChar;

    }

    return dstStr;
}

int main()
{
    std::wstring input_str = L"55555。你好。";

    std::wstring output_str = DoubleByteCharToSingleByteChar_Unicode(input_str);

    std::wcout << output_str << std::endl;
}