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++;
}
}
}
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++实现JNI接口需要注意的事项
- 如何组织构建多文件 C 语言程序(二)
- 关于指针的一些事情
- 如何写好 C main 函数
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua和C语言的交互详解
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解