C++数字全转半角
#include <iostream>
#include <string>
// 将 UTF-8 编码的全角数字字符转换为半角数字字符
std::string fullWidthToHalfWidth(const char* text) {
std::string result;
while (*text) {
// 检查是否是 UTF-8 编码的全角数字字符(0xEF 0xBC 0x90 - 0xEF 0xBC 0x99)
if (static_cast<unsigned char>(text[0]) == 0xEF &&
static_cast<unsigned char>(text[1]) == 0xBC &&
static_cast<unsigned char>(text[2]) >= 0x90 &&
static_cast<unsigned char>(text[2]) <= 0x99) {
// 将全角数字转换为半角数字
char halfWidthChar = '0' + (text[2] - 0x90);
result += halfWidthChar;
// 跳过当前全角字符的 3 字节
text += 3;
} else {
// 直接添加非全角字符
result += *text;
++text;
}
}
return result;
}