C++ 代码检测 UTF-16LE、UTF-16BE、UTF32LE、UTF32BE、UTF8 和 GB2312 编码
#include
bool isUTF16LE(const std::vector
bool isUTF16BE(const std::vector
bool isUTF32LE(const std::vector
bool isUTF32BE(const std::vector
bool isUTF8(const std::vector
bool isGB2312(const std::vector
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 著作权归作者所有。请勿转载和采集!