浠€涔堟槸鏂囦欢鐨凚OM澶达紝鍙夿OM澶存湁鍝簺鍧戯紵

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了浠€涔堟槸鏂囦欢鐨凚OM澶达紝鍙夿OM澶存湁鍝簺鍧戯紵相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/html' title='html'>html   鐗圭偣   cti   bsp   blank   turn   璁剧疆   span   nic   

鏉ヨ嚜锛?a href="https://www.cnblogs.com/qinmengjiao123-123/p/8325646.html" target="_blank">https://www.cnblogs.com/qinmengjiao123-123/p/8325646.html

Unicode瑙勮寖涓帹鑽愮殑鏍囪瀛楄妭椤哄簭鐨勬柟娉曟槸BOM銆侭OM涓嶆槸“Bill Of Material”鐨凚OM琛紝鑰屾槸Byte Order Mark銆?
--------------------------------------------------------------------------------------------------------------
1.浠€涔堟槸BOM锛?BOM鏄敤鏉ュ垽鏂枃鏈枃浠舵槸鍝竴绉峌nicode缂栫爜鐨勬爣璁帮紝鍏舵湰韬槸涓€涓猆nicode瀛楃锛?/span>"uFEFF"锛夛紝浣嶄簬鏂囨湰鏂囦欢澶撮儴銆?
鍦ㄤ笉鍚岀殑Unicode缂栫爜涓紝瀵瑰簲鐨刡om鐨勪簩杩涘埗瀛楄妭濡備笅锛?
Bytes Encoding
FE FF UTF16BE
FF FE UTF16LE
EF BB BF UTF8

鎵€浠ユ垜浠彲浠ユ牴鎹枃浠跺ご閮ㄧ殑鍑犱釜瀛楄妭鍜屼笂闈㈢殑琛ㄦ牸瀵瑰簲鏉ュ垽鏂鏂囦欢鏄摢绉嶇紪鐮佸舰寮忋€?
2. 濡備綍鏌ョ湅浣犳枃浠剁殑BOM瀛楃锛?BOM澶村湪璁颁簨鏈腑鏄湅涓嶅埌鐨?
浠TF8涓轰緥锛屾柊寤轰竴涓?.txt鏂囦欢锛岀劧鍚庢墦寮€鏂囦欢 杈撳叆 hello world 鐒跺悗鍙﹀瓨涓?姝ゆ椂鏇存敼鏂囦欢鐨勭紪鐮佹牸寮?鐢遍粯璁ょ殑ANSI 鍗矴B2312(鍥芥爣2312)缂栫爜鏀规垚UTF8

鐒跺悗閫氳繃涓嬮潰鐨刵ode浠g爜锛屾潵鎵撳嵃璇ユ枃鏈枃浠剁殑buffer 灏卞彲浠ョ湅鍒版垜浠墍璇寸殑BOM澶村瓧绗?

let fs = require(鈥榝s鈥?span style="color: #000000;">);

fs.readFile(鈥?/1.txt鈥?span style="color: #000000;">,function(err,data){
    console.log(data)
})

// <Buffer ef bb bf 68 65 6c 6c 6f 20 77 6f 72 6c 64>
// 鍓嶄笁涓瓧鑺傚氨鏄搴旂殑鎴戜滑UTF8缂栫爜鐨勬枃鏈殑BOM澶村瓧绗?/span>

3.瀛樺湪鐨勯棶棰橈紵
铏界劧BOM瀛楃璧峰埌浜嗘爣璁版枃浠剁紪鐮佺殑浣滅敤浣嗘槸浠栧苟涓嶅睘浜庢枃浠剁殑鍐呭閮ㄥ垎锛屾墍浠ヤ細浜х敓涓€浜涢棶棰橈細

1.鍦ㄦ煇浜涗娇鐢ㄥ満鏅笅灏变細鏈夐棶棰樸€備緥濡傛垜浠妸鍑犱釜JS鏂囦欢鍚堝苟鎴愪竴涓枃浠跺悗锛屽鏋滄枃浠朵腑闂村惈鏈塀OM瀛楃锛屽氨浼氬鑷存祻瑙堝櫒JS璇硶閿欒銆?
2.php灏变笉鑳借瘑鍒玝om澶达紝PHP骞朵笉浼氬拷鐣OM锛屾墍浠ュ湪璇诲彇銆佸寘鍚垨鑰呭紩鐢ㄨ繖浜涙枃浠舵椂锛屼細鎶夿OM浣滀负璇ユ枃浠跺紑澶存鏂囩殑涓€閮ㄥ垎銆傛牴鎹祵鍏ュ紡璇█鐨勭壒鐐癸紝杩欎覆瀛楃灏嗚鐩存帴鎵ц锛堟樉绀猴級鍑烘潵銆傜敱姝ら€犳垚鍗充娇椤甸潰鐨?top padding 璁剧疆涓?锛屼篃鏃犳硶璁╂暣涓綉椤电揣璐存祻瑙堝櫒椤堕儴锛屽洜涓哄湪html涓€寮€澶存湁杩?涓瓧绗﹀憿锛?
4.濡備綍鍘绘帀UTF8缂栫爜鐨勮浜嬫湰鏂囦欢鐨凚OM澶?鏂规硶涓€锛氬湪鏂囦欢鍙﹀瓨涓虹殑鏃跺€欓€夋嫨鏃燘OM澶寸殑UTF8缂栫爜

鏂规硶浜岋細浣跨敤node涓殑鏂囦欢妯″潡鑾峰彇鏂囦欢鐨刡uffer鏁版嵁骞跺幓鎺夊墠涓変釜瀛楄妭锛屼唬鐮佸涓嬶細

function deleteUtf8BomHead(path) {
    let buf = fs.readFileSync(path);
    if (buf[0] == 0xef && buf[1] == 0xbb && buf[2] == 0xbf) {
        buf = buf.slice(3)
    }
    return buf
}

console.log(deleteUtf8BomHead(鈥?/1.txt鈥?);// <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
  
// 灏咷BK缂栫爜鐨刡uffer杞寲鎴恥tf8鐨勫瓧绗︿覆
// 鍦ㄨ繖閲屾垜浠寮曞叆涓€涓簱 iconv-lite
let iconv = require(鈥榠conv-lite鈥?span style="color: #000000;">);
fs.readFile(鈥?/2.txt鈥?span style="color: #000000;">,function (err, data) {
console.log(data,鈥榪q鈥?span style="color: #000000;">);
let str = iconv.decode(data,鈥榞bk鈥?span style="color: #000000;">);
console.log(str);
})

 

以上是关于浠€涔堟槸鏂囦欢鐨凚OM澶达紝鍙夿OM澶存湁鍝簺鍧戯紵的主要内容,如果未能解决你的问题,请参考以下文章

Python3鍩虹 getatime getctime getmtime 鏂囦欢鐨勬渶杩戣闂?+ 灞炴€т慨鏀?+ 鍐呭淇敼鏃堕棿

浠€涔堟槸sigmoid婵€娲诲嚱鏁帮紵

浠€涔堟槸璐績绠楁硶? 鈥嬩辅CSDN缂栫▼鐧剧

浠€涔堟槸浜屽弶鏍?鈥嬩辅CSDN缂栫▼鐧剧

浠€涔堟槸async鍜宎wait?(椤圭洰涓亣鍒扮殑)

浠€涔堟槸骞垮害浼樺厛鎼滅储锛熶辅CSDN缂栫▼鐧剧