2012年3月9日

BOM (Byte Order Mark) 與 UTF-8

一些個人筆記,省得以後每次遇到還得重新上網搜尋做功課。


Byte Order Mark 在 UTF-16 Big Endian 是 "FE FF",Little Endian 是 "FF FE"。其中 Mac 系統主要是 Big Endian, PC 系統則是使用 Little Endian。

UTF-8 由於本身特性,不管 Big Endian 或 Little Endian,其 BOM 一律為 "EF BB BF"。

不過個人建議,文字文件要儲存的話,盡量以 UTF-8 格式存。而以 UTF-8 儲存的文件,盡量不要放 BOM 進去。為什麼呢?因為 UTF-8 的一個特色就是能與 ASCII 系統相容,如果文件中沒有包含非拉丁字元的話;要是放了 BOM 進去,就破壞了這相容性。

在 VIM 中的 BOM 相關操作

加入 BOM

:set bomb

移除 BOM

:set nobomb

查詢 BOM 狀態

:set bomb?

在 Terminal / Console 下的 BOM 相關操作

查詢包含 UTF-8 BOM 的檔案

$ grep -rIl $'^\xEF\xBB\xBF' [FILE]

找出並移除 UTF-8 的 BOM

$ grep -rIl $'^\xEF\xBB\xBF' [FILE] | xargs sed -i 's/^\xEF\xBB\xBF//;g'

編碼的轉換

$ iconv -f [FROM_ENCODING] -t [TO_ENCODING] [FILE]

其中 ENCODING 的部份可以是以下數值:

  • UTF-8 UTF8
  • UTF-16BE
  • UTF-16LE
  • UCS-2BE UNICODEBIG
  • UCS-2LE UNICODELITTLE

更多 ENCODING 代碼可以透過 iconv --list 查詢。

2 則留言:

  1. The material is good for device parts that have to be flexible, such as touchscreens. Proponents of the graphene choice declare it is Slippers one of the|is amongst the|is doubtless one of the} most flexible of 3D-applicable supplies. We are interested to proceed with your help for our analysis on 3D-printing. Our institute is entitled with nicely furnished "RESEARCH AND DEVELOPMENT CENTER" .

    回覆刪除
  2. The benchtop CNC Disposable Shower Caps router is right for anyone looking for a user-friendly machine that doesn’t sacrifice high quality for affordability. With a piece envelope of 2′ x 3′ or 2′ x 4′, this machine can handle extensive range|a variety} of prototyping and small manufacturing duties. Laguna Tools offers superior high quality and efficiency at an inexpensive value. Take your store to the next stage with one of our many turn-key solutions and start producing high-quality products.

    回覆刪除

Related Posts Plugin for WordPress, Blogger...