0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Node.jsでバイナリファイルをhex化して改行入れて見やすくするコード

Posted at
const fs = require("fs")

const filePath = process.argv[2]
const CUT_LEN = 56

const ttt = fs.readFileSync(filePath)
const list = Array.from(Uint8Array.from(ttt))
  .map(v => {
    const v2 = v.toString(16).toUpperCase()
    return (v2.length === 1) ? '0' + v2 : v2
  })
const num = Math.floor(list.length / CUT_LEN)

for (let i = 0; i < num; i++) {
  const start = i * CUT_LEN
  const end = start + CUT_LEN
  console.log(list.slice(start, end).join(' '))
}

// 割り切れない場合、端数処理
if ((list.length % CUT_LEN) !== 0) {
  const start = num * CUT_LEN
  const end = list.length
  console.log(list.slice(start, end).join(' '))
}

// 上記コードのファイル名がmmm.jsだったとしたら、下記となる。
// $ node mmm.js 'r:file-name.DAT'

結構適当に書いたので、変数名とかリーダブルの逆を行っている気がする。。

今回のサンプルは、56byteずつ切って表示している。

ファイル名は自分の環境に合わせて変えてほしい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?