本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – Unicode编码下的全角字符转半角字符
原文链接:https://www.stubbornhuang.com/1849/
发布于:2021年12月04日 12:02:46
修改于:2021年12月04日 12:02:46

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;
}
当前分类随机文章推荐
- C++ - vector存储动态指针时正确释放内存 阅读4468次,点赞0次
- Windows - 使用类的成员函数作为Win32窗口消息回调处理函数WindowProc 阅读383次,点赞0次
- C++STL容器 - std::vector元素访问方式总结 阅读306次,点赞0次
- C++ - 字节数组byte[]或者unsigned char[]与double的相互转换 阅读599次,点赞0次
- C++ - 格式化json字符串,方便展示json字符串的层次结构 阅读1129次,点赞0次
- C++ – Unicode编码下的全角字符转半角字符 阅读1065次,点赞0次
- C++ - Windows/Linux生成uuid(通用唯一识别码) 阅读552次,点赞1次
- C++11 - 解析并获取可变参数模板中的所有参数 阅读661次,点赞0次
- C++11 - 基于无锁队列的单生产者单消费者模型 阅读4202次,点赞1次
- C++ - 使用宏区分不同系统平台、不同编译器、不同编译模式等编译期宏使用总结 阅读586次,点赞0次
全站随机文章推荐
- Python3爬虫 - requests库的requests.exceptions所有异常详细说明 阅读4037次,点赞1次
- C++11 - 解析并获取可变参数模板中的所有参数 阅读661次,点赞0次
- WordPress - count_user_posts函数,获取某个用户发表的文章数量 阅读308次,点赞0次
- Mediapipe - 全身包含身体、手部、面部所有关键点标注位置对应图 阅读2448次,点赞1次
- WordPress - 在用户登录页面添加自定义提示信息 阅读1085次,点赞0次
- Python - 字典dict遍历方法总结 阅读84次,点赞0次
- 资源分享 - Geometric tools for computer graphics(Philip J. Schneider, and David H. Eberly)英文高清PDF下载 阅读2638次,点赞0次
- 资源分享 - Speech and Language Processing - An Introduction to Natural Language Processing, Computational Linguistics, and Speech Recognition , Second Edition 英文高清PDF下载 阅读157次,点赞0次
- OpenCV - 打开视频文件,并对其中的每一帧图像进行Canny算子边缘化提取,并将结果保存为视频文件 阅读2415次,点赞0次
- Python - 使用websockets库构建websocket服务器 阅读1101次,点赞0次
评论
153