osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Writing bytes to stdout reverses the bytes


On 2018-08-20 17:07, Akkana Peck wrote:
>> Thomas Jollans <tjol at tjol.eu>:
>>> Wonderful. Now why don't we all forget about hexdump and use xxd? ;-)
> 
> Marko Rauhamaa writes:
>> Fedora:
>>
>>    $ xxd
>>    bash: xxd: command not found
>>    $ hd
>>    bash: hd: command not found
>>    $ od -Ax -tx1z -v <<<hello
>>    000000 68 65 6c 6c 6f 0a                                >hello.<
>>    000006
> 
> Or, since this is Python, why be frustrated over imperfect command
> output when you can write something that gives whatever format you
> like best?  Long ago the old Unix od used to give something like this,
> and when I was learning Python writing it seemed like a good exercise:
> 
> $ bdump hello
>         >    h    e    l    l    o    .    <   ^J 
>   20   3e   68   65   6c   6c   6f   2e   3c    a 
>   32   62  104  101  108  108  111   46   60   10 
> 
> https://github.com/akkana/scripts/blob/master/bdump
> 
>         ...Akkana
> 

This feels like to could be a one-liner... (different output to yours)

python3 -c 'W = 16; import sys, itertools as it; sum(print("
".join(map("{:02x}".format, b))) or 0 for b in it.takewhile(bool,
map(sys.stdin.buffer.read, it.repeat(W))))'

or, with offset numbers and an ASCII column,

python3 -c 'W = 16; import sys, itertools as it; sum(print(f"{W*i:08X}
", " ".join(map("{:02x}".format, bb)).ljust(3*W+1), "".join(chr(b) if
0x20 <= b <= 0x7e else "." for b in bb)) or 0 for i, bb in
enumerate(it.takewhile(bool, map(sys.stdin.buffer.read, it.repeat(W)))))'