Quick actions

cmd+k|ctrl+k

Navigation

Languages

Hexdump

Snippet info

Language

Nim

Visibility

public

Author

oderwat

Created

2016-08-07T20:56:36Z

Updated

2016-08-07T20:56:36Z

import strutils

proc hexDump*(s: string | cstring, cols: int = 16, offs: int = 2): string =
  result = newStringOfCap(s.len * 3 - 1 + (s.len div cols + 1) * (offs + 2))
  var newLine = true
  for idx, c in s:
    if idx > 0:
      if idx mod cols == 0:
        result.add "\n"
        newLine = true
      else:
        result.add ' '

    if newLine:
      newLine = false
      result.add idx.toHex offs
      result.add ": "
    result.add ord(c).toHex 2

echo hexDump("0123456789ABCDEF\nHello World! The Hexdump!")
INFO