#include #include #include

bool isUTF16LE(const std::vector& bytes) { // UTF-16LE BOM (Byte Order Mark): 0xFF 0xFE return (bytes.size() >= 2 && bytes[0] == 0xFF && bytes[1] == 0xFE); }

bool isUTF16BE(const std::vector& bytes) { // UTF-16BE BOM (Byte Order Mark): 0xFE 0xFF return (bytes.size() >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF); }

bool isUTF32LE(const std::vector& bytes) { // UTF-32LE BOM (Byte Order Mark): 0xFF 0xFE 0x00 0x00 return (bytes.size() >= 4 && bytes[0] == 0xFF && bytes[1] == 0xFE && bytes[2] == 0x00 && bytes[3] == 0x00); }

bool isUTF32BE(const std::vector& bytes) { // UTF-32BE BOM (Byte Order Mark): 0x00 0x00 0xFE 0xFF return (bytes.size() >= 4 && bytes[0] == 0x00 && bytes[1] == 0x00 && bytes[2] == 0xFE && bytes[3] == 0xFF); }

bool isUTF8(const std::vector& bytes) { // UTF-8 BOM (Byte Order Mark): 0xEF 0xBB 0xBF return (bytes.size() >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF); }

bool isGB2312(const std::vector& bytes) { // GB2312 does not have a BOM, so we can only check for valid GB2312 characters // Here, we assume that if all bytes are within the range of 0x81-0xFE, it is GB2312 encoded. for (unsigned char byte : bytes) { if (!(byte >= 0x81 && byte <= 0xFE)) { return false; } } return true; }

int main() { std::ifstream inputFile("input.txt", std::ios::binary); // 从二进制文件中读取字节流 if (!inputFile) { std::cout << "无法打开文件" << std::endl; return 1; }

std::vector<unsigned char> bytes;
unsigned char byte;
while (inputFile >> std::noskipws >> byte) {
    bytes.push_back(byte);
}

if (isUTF16LE(bytes)) {
    std::cout << "UTF-16LE 编码" << std::endl;
} else if (isUTF16BE(bytes)) {
    std::cout << "UTF-16BE 编码" << std::endl;
} else if (isUTF32LE(bytes)) {
    std::cout << "UTF-32LE 编码" << std::endl;
} else if (isUTF32BE(bytes)) {
    std::cout << "UTF-32BE 编码" << std::endl;
} else if (isUTF8(bytes)) {
    std::cout << "UTF-8 编码" << std::endl;
} else if (isGB2312(bytes)) {
    std::cout << "GB2312 编码" << std::endl;
} else {
    std::cout << "未知编码" << std::endl;
}

return 0;

}


原文地址: https://www.cveoy.top/t/topic/p8gy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录