———————————–
通过前2字节判断:
编码 | 16进制数 |
unicode | FF FE 或 FE FF |
utf-8 | EF BB |
代码:
var fs=require('fs')
fs.readFile('code.txt',function(err,buffer){
if(buffer[0]==0xff&&buffer[1]==0xfe){
console.log('unicode')
}else if(buffer[0]==0xfe&&buffer[1]==0xff){
console.log('unicode')
}else if(buffer[0]==0xef&&buffer[1]==0xbb){
console.log('utf8')
}else{
console.log('else')
}
})
参考:https://www.cnblogs.com/wl843022618/p/6767066.html?utm_source=itdadao&utm_medium=referral