c++ 中判断字符是 ASCII 编码还是 GBK 编码

本文介绍了如何在C++中通过isASCII和isGBK函数判断一个字符是ASCII编码还是GBK编码,通过实例展示了如何使用这些函数进行字符类型的检查。
摘要由CSDN通过智能技术生成

在C++中,一个字符是ASCII编码还是GBK编码,取决于其在ASCII范围内还是在GBK范围内。

ASCII编码的范围是0到127,而GBK编码是双字节编码,包含了更广泛的字符范围。

#include <iostream>
#include <string>
#include <locale>

bool isASCII(char c) {
    // ASCII编码的范围是0到127
    return static_cast<unsigned char>(c) <= 127;
}

bool isGBK(char c) {
    // GBK编码的范围比ASCII广泛,可以根据实际情况扩展判断条件
    return !isASCII(c);
}

int main() {
    // 测试示例
    char asciiChar = 'A';
    char gbkChar = '你';

    // 判断字符是ASCII编码还是GBK编码
    std::cout << "字符 '" << asciiChar << "' 是ASCII编码吗?" << std::boolalpha << isASCII(asciiChar) << std::endl;
    std::cout << "字符 '" << gbkChar << "' 是GBK编码吗?" << std::boolalpha << isGBK(gbkChar) << std::endl;

    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的 C++ 算法,用于将 GB18030 编码转换成 Unicode 编码: ```c++ #include <iostream> #include <string> #include <vector> typedef unsigned char BYTE; typedef unsigned short WORD; typedef unsigned int DWORD; // GB18030 编码转换成 Unicode 编码 std::wstring GB18030ToUnicode(const std::string& str) { std::wstring result; std::vector<BYTE> bytes(str.begin(), str.end()); int i = 0; while (i < bytes.size()) { if (bytes[i] <= 0x7F) { // ASCII 字符 result += (wchar_t)bytes[i]; i++; } else if (bytes[i] >= 0x81 && bytes[i] <= 0xFE) { // 双字节字符 if (i + 1 < bytes.size()) { WORD w = ((WORD)bytes[i] << 8) | ((WORD)bytes[i + 1]); if (w >= 0x8140 && w <= 0xFEFE && w != 0x817F && w != 0x8180) { // GBK/GB2312 编码范围内的汉字 result += (wchar_t)(0x4E00 + (w - 0x8140) / 0x100 * 0x40 + (w - 0x8140) % 0x100); } else if (w >= 0x8130 && w <= 0xA0FE) { // GB18030 编码范围内的汉字 DWORD dw = ((DWORD)w - 0x8130) / 10 * 0x100 + ((DWORD)w - 0x8130) % 10 + 0x10000; result += (wchar_t)dw; } i += 2; } else { break; } } else if (bytes[i] >= 0x80 && bytes[i] <= 0xBF) { // 单字节字符,不是汉字 result += (wchar_t)bytes[i]; i++; } else if (bytes[i] >= 0xC0 && bytes[i] <= 0xDF) { // 双字节字符,不是汉字 if (i + 1 < bytes.size()) { WORD w = ((WORD)bytes[i] << 8) | ((WORD)bytes[i + 1]); result += (wchar_t)(0x80 + (w - 0xC0A0)); i += 2; } else { break; } } else if (bytes[i] >= 0xE0 && bytes[i] <= 0xEF) { // 三字节字符 if (i + 2 < bytes.size()) { DWORD dw = ((DWORD)bytes[i] << 16) | ((DWORD)bytes[i + 1] << 8) | (DWORD)bytes[i + 2]; result += (wchar_t)dw; i += 3; } else { break; } } else { break; } } return result; } ``` 下面是一个简单的 C++ 算法,用于将 Unicode 编码转换成 GB18030 编码: ```c++ #include <iostream> #include <string> #include <vector> typedef unsigned char BYTE; typedef unsigned short WORD; typedef unsigned int DWORD; // Unicode 编码转换成 GB18030 编码 std::string UnicodeToGB18030(const std::wstring& str) { std::string result; for (int i = 0; i < str.size(); i++) { if (str[i] <= 0x7F) { // ASCII 字符 result += (char)str[i]; } else if (str[i] >= 0x4E00 && str[i] <= 0x9FA5) { // GBK/GB2312 编码范围内的汉字 BYTE b1 = (BYTE)((str[i] - 0x4E00) / 0x40 + 0x81); BYTE b2 = (BYTE)((str[i] - 0x4E00) % 0x40 + 0x40); result += b1; result += b2; } else { // GB18030 编码范围内的汉字 DWORD dw = (DWORD)str[i]; if (dw >= 0x10000 && dw <= 0x10FFFF) { dw -= 0x10000; BYTE b1 = (BYTE)(dw / 0x1000 + 0x90); BYTE b2 = (BYTE)((dw % 0x1000) / 0x40 + 0x81); BYTE b3 = (BYTE)((dw % 0x1000) % 0x40 + 0x30); result += b1; result += b2; result += b3; } } } return result; } ``` 需要注意的是,这两个算法只是简单的示例代码,不能处理所有情况。在实际应用,需要根据具体需求进行修改和完善。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值