-1

The first block of code below is an example of how I want my output to look like when I run my hexdump command in C. Atm, I don't know how to get the 3 columns of double spaces and I don't know how to add the ASCII representation and "." for non-ASCII of the bytes on the right.

hexdump -C /etc/passwd

00000000  72 6f 6f 74 3a 78 3a 30  3a 30 3a 72 6f 6f 74 3a  |root:x:0:0:root:|
00000010  2f 72 6f 6f 74 3a 2f 62  69 6e 2f 62 61 73 68 0a  |/root:/bin/bash.|
00000020  64 61 65 6d 6f 6e 3a 78  3a 31 3a 31 3a 64 61 65  |daemon:x:1:1:dae|
00000030  6d 6f 6e 3a 2f 75 73 72  2f 73 62 69 6e 3a 2f 62  |mon:/usr/sbin:/b|
00000040  69 6e 2f 73 68 0a 62 69  6e 3a 78 3a 32 3a 32 3a  |in/sh.bin:x:2:2:|
00000050  62 69 6e 3a 2f 62 69 6e  3a 2f 62 69 6e 2f 73 68  |bin:/bin:/bin/sh|

This is my actual output:

00000000 2f 2a 20 68 65 6c 6c 6f 2e 63 20 2a 2f a. a. 23
00000010 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68
00000020 3e a. a. 69 6e 74 20 6d 61 69 6e 28 69 6e 74 20
00000030 61 72 67 63 2c 20 63 68 61 72 20 2a 61 72 67 76
00000040 5b 5d 29 a. 7b a. 20 20 70 75 74 73 28 22 48 65
00000050 6c 6c 6f 20 77 6f 72 6c 64 21 5c 6e 22 29 3b a.
00000060 20 20 72 65 74 75 72 6e 20 30 3b a. 7d a.
0000006e

thanks

  • 3
    Which bits _do_ you know how to do? What code do you have so far? – Useless Jul 16 '18 at 14:52
  • 1
    The two example hex dumps seem to be of unrelated files — or if your program is also a dump of the password file, then you've got dramatic problems with your main read and print code. Use `%.2x` to get `0a` produced for 0x0A or `'\n'` (or use `%.2X` if you prefer upper case hex). You should check that your dump works on accented characters (or a binary file such as its own executable) — you can easily end up with badly handled formatting if your compiler uses a signed plain `char` type. – Jonathan Leffler Jul 16 '18 at 15:26

1 Answers1

1

I don't know how to get the 3 columns of double spaces

Just emit an extra space in the appropriate place.

The first one (between the offset and the first octet) is always there. The second one is emitted before the 8th octet if there is one. The third one is also always there.

You can either build them into a format string or bake them into your octet-formatting loop.

I don't know how to add the ASCII representation and "." for non-ASCII of the bytes on the right

Roughly, by using isprint() and then printing toprint instead of the raw character

char toprint = isprint(raw) ? raw : '.';
Useless
  • 64,155
  • 6
  • 88
  • 132
  • Actually, all three 'extra blanks' are always present. If the last line is less than 8 bytes, you'll output padding 'triple spaces' (plus the extra space) to get to the RHS decoded output. – Jonathan Leffler Jul 16 '18 at 15:29
  • True, but the code _could_ do something different for aligning the right panel than for aligning octets in the central panel - without seeing what OP has so far I'm hesitant to make assumptions about their formatting logic. – Useless Jul 16 '18 at 16:15