string 全角转半角

string ToHalf1(string str) {
    string result = "";
    unsigned char tmp; unsigned char tmp1;
    for (unsigned int i = 0; i < str.length(); i++) {
        tmp = str[i];
        tmp1 = str[i + 1];
       // cout << "uchar:" << (int) tmp << endl;
        if (tmp == 163) {///第一个字节是163,标志着是全角字符
            result += (unsigned char) str[i + 1] - 128;
            i++;
            continue;
        } else if (tmp > 163) {//汉字
            result += str.substr(i, 2);
            i++;
            continue;
        } else if (tmp == 161 && tmp1 == 161) {///处理全角空格
            result += "";
            i++;
        } else {
            result += str.substr(i, 1); } } return result;
}
std::string utf8_tohalf(std::string input)
{
    std::string temp;
    for (size_t i = 0; i < input.size(); i++) {
        if (((input[i] & 0xF0) ^ 0xE0) == 0) {
            int old_char = (input[i] & 0xF) << 12 | ((input[i + 1] & 0x3F) << 6 | (input[i + 2] & 0x3F));
            if (old_char == 0x3000) { // blank
                char new_char = 0x20;
                temp += new_char;
            } else if (old_char >= 0xFF01 && old_char <= 0xFF5E) { // full char
                char new_char = old_char - 0xFEE0;
                temp += new_char;
            } else { // other 3 bytes char
                temp += input[i];
                temp += input[i + 1];
                temp += input[i + 2];
            }
            i = i + 2;
        } else {
            temp += input[i];
        }
    }
    return temp;
}

猜你喜欢

转载自blog.csdn.net/qingfenglu/article/details/82191321