catnon-printing-characters

cat a file's content, show non-printable chars as \xNN


is there any linux command line tool to cat any file's content which may be mixed with UTF-8 string and non-printable chars, but also show non-printable chars as \xNN?

such as abc\xa1defg,

PS: I don't need the two column output like xxd produces, or the the space separated output that od produces.

So far, the most close result is: od -t c FILE

0000000   S   Q   L   i   t   e       f   o   r   m   a   t       3  \0
0000020 020  \0  \n   \t \0  \0  \0  \0  \0  \0  \0 001  \0  \0  \0 004
0000040  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0 001  \0  \0  \0 004

But what I want is is like this

SQLite format 3\0\020\0
       \0\0.....

Found a similar question: https://unix.stackexchange.com/questions/176111/how-to-dump-a-binary-file-as-a-c-c-string-literal


Solution

  • Not perfect, but nearby:

    hexdump -e '16 "%_c" "\n"' file.sqlite
    

    -e specifies the output format, 16 = number of chars per line (iteration count), for _c see man page:

    Output characters in the default character set. Nonprinting characters are displayed in three character, zero-padded octal, except for those representable by standard escape notation (see above), which are displayed as two character strings.

    Output:

    SQLite format 3\0
    200\0001001\0@  \0\0006�\0\0\0\a
    



    If you really want the output as described in your question, you'll have to roll your own programm. Here is a quick and easy solution:

    #!/usr/bin/env python3
    import sys
    
    if len(sys.argv) < 2:
        exit(1)
    
    with open(sys.argv[1], "rb") as f:
        while True:
            b = f.read(1)
            if not b:
                break
            c = ord(b)
            print(f'\\x{c:02x}' if (c < 32 or c > 126 and c < 161) else f'{c:c}', end='')
    

    With a test file generated by this two liner

    with open('test.dat','wb')as f:
        f.write(bytearray([i for i in range(256)]))
    

    the output of myhexdump test.dat will be:

    \x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ