C++ 代码检测 UTF-16LE 无签名编码文件
下面是一个简单的 C++ 代码示例,用于判断一个文件是否为 UTF-16LE 无签名编码:
#include <iostream>
#include <fstream>
bool isUTF16LE(const std::string& filename) {
std::ifstream file(filename, std::ios::binary);
if (!file) {
std::cerr << "Failed to open file: " << filename << std::endl;
return false;
}
uint8_t bom[2];
file.read(reinterpret_cast<char*>(bom), sizeof(bom));
// UTF-16LE BOM: 0xFF 0xFE
if (bom[0] == 0xFF && bom[1] == 0xFE) {
return true;
}
return false;
}
int main() {
std::string filename = "test.txt"; // 替换为你要检查的文件名
if (isUTF16LE(filename)) {
std::cout << filename << " is UTF-16LE without signature." << std::endl;
} else {
std::cout << filename << " is not UTF-16LE without signature." << std::endl;
}
return 0;
}
请将test.txt替换为你要检查的文件名,并确保该文件与代码文件位于相同的目录中。代码将检查文件的前两个字节,如果它们等于UTF-16LE的BOM(0xFF 0xFE),则判断文件是UTF-16LE无签名编码文件。
原文地址: https://www.cveoy.top/t/topic/p95U 著作权归作者所有。请勿转载和采集!