C++全角与半角互转

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++;
			}
		}
	}


  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值