您的位置:首页 > 编程语言 > C语言/C++

C++全角与半角互转

2016-03-11 10:30 246 查看
1.全角:指一个字符占用两个标准字符位置。汉字字符和规定了全角的英文字符及国标GB2312-80中的图形符号和特殊字符都是全角字符。一般的系统命令是不用全角字符的,只是在作文字处理时才会使用全角字符。

2.半角:指一字符占用一个标准的字符位置。通常的英文字母、数字键、符号键都是半角的,半角的显示内码都是一个字节。在系统内部,以上三种字符是作为基本代码处理的,所以用户输入命令和参数时一般都使用半角。

3.全角与半角在计算机中的表示:据我所知,全角的第一个字节是163(我用-93),然后第二个字节与半角相差128。全角空格和半角空格也要考虑进去。

4.C/C++版本:

static inline void toDBS(const std::string &oriStr, std::string &dstStr)
<span style="white-space:pre"> </span>{
dstStr = "";
unsigned char tmp, tmp1;
for(unsigned int i = 0; i < oriStr.length(); i++)
{
tmp = oriStr[i];
tmp1 = oriStr[i + 1];
if(tmp == 163)
{
dstStr += (unsigned char)oriStr[i + 1] - 128;
i++;
continue;
}
else if(tmp > 163)
{
dstStr += tmp ;
dstStr += tmp1;
i++;
continue;
}
else if(tmp == 161 && tmp1 == 161)
{
dstStr += "";
i++;
}
else
{
dstStr += tmp;
}
}
}

static inline void toQBS(const std::string &oriStr, std::string &dstStr)
{
dstStr = "";
for(unsigned int i = 0; i < oriStr.length(); i++)
{
if(oriStr[i] <= 127 && oriStr[i] > 32)
{
dstStr += (char)163;
dstStr += (char)(oriStr[i] + 128);
}
else if(oriStr[i] >= 163)
{
dstStr += oriStr[i] + oriStr[i+1];
i++;
}
else if(oriStr[i] == 32)
{
dstStr += 161;
dstStr += 161;
}
else
{
dstStr += oriStr[i] + oriStr[i+1];
i++;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  全角 C++ c语言