Here another `hexdump()`:
    
    
    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!")
    

Output:
    
    
    00: 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46
    10: 0A 48 65 6C 6C 6F 20 57 6F 72 6C 64 21 20 54 68
    20: 65 20 48 65 78 64 75 6D 70 21
    

[RunIt](https://glot.io/snippets/eha9vga43x)

Reply via email to