UTF-8(BOM付き)しか読み込めないプログラムがあり、どうしても そのプログラムと連携したかったため、必要な機能を作成しました。
ソース
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const BOM = [0xEF, 0xBB, 0xBF] | |
const encoder = new TextEncoder() | |
const data = encoder.encode("Hello world\n") | |
const txt = BOM.join() + ',' + data.join() | |
const arr = txt.split(',').map( (str:string):number => parseInt(str)) | |
const uint = new Uint8Array(arr) | |
await Deno.writeFile("UTF-8(BOM付き).txt", uint) |
実行
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
deno run --allow-write BOM.ts |
結果
メモ帳で開いてみると、画面右下に確かに「BOM付き」と表示されています。
お世話になったサイト
PowerShellの文字コードについて解説!文字化けで困らないための対処法 | テックマガジン from FEnetインフラ
ちなみにDeno.writeTextFileのオプションを指定して同様の事ができないか?と思いましたが、今のところできないようです。
0 件のコメント:
コメントを投稿