C++ 代码判断文件编码格式 (UTF-16, UTF-32, UTF-8, GB2312)
#include
bool isUTF16LE(std::vector
bool isUTF16BE(std::vector
bool isUTF32LE(std::vector
bool isUTF32BE(std::vector
bool isUTF8(std::vector
bool isGB2312(std::vector
bool isUTF16LENoBOM(std::vector
bool isUTF16BENoBOM(std::vector
bool isUTF32LENoBOM(std::vector
bool isUTF32BENoBOM(std::vector
bool isUTF8NoBOM(std::vector
int main() {
std::ifstream file("example.txt", std::ios::binary);
std::vector
if (isUTF16LE(data)) {
std::cout << "UTF16LE" << std::endl;
} else if (isUTF16BE(data)) {
std::cout << "UTF16BE" << std::endl;
} else if (isUTF32LE(data)) {
std::cout << "UTF32LE" << std::endl;
} else if (isUTF32BE(data)) {
std::cout << "UTF32BE" << std::endl;
} else if (isUTF8(data)) {
std::cout << "UTF8" << std::endl;
} else if (isGB2312(data)) {
std::cout << "GB2312" << std::endl;
} else if (isUTF16LENoBOM(data)) {
std::cout << "UTF16LE无签名" << std::endl;
} else if (isUTF16BENoBOM(data)) {
std::cout << "UTF16BE无签名" << std::endl;
} else if (isUTF32LENoBOM(data)) {
std::cout << "UTF32LE无签名" << std::endl;
} else if (isUTF32BENoBOM(data)) {
std::cout << "UTF32BE无签名" << std::endl;
} else if (isUTF8NoBOM(data)) {
std::cout << "UTF8无签名" << std::endl;
} else {
std::cout << "未知编码" << std::endl;
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/p8gT 著作权归作者所有。请勿转载和采集!