Re: Writing bytes to stdout reverses the bytes

2018-08-20 Thread Thomas Jollans
On 2018-08-20 17:07, Akkana Peck wrote:
>> Thomas Jollans :
>>> 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 <<>00 68 65 6c 6c 6f 0a>hello.<
>>06
> 
> 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
> >hello.<   ^J 
>   20   3e   68   65   6c   6c   6f   2e   3ca 
>   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)'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Writing bytes to stdout reverses the bytes

2018-08-20 Thread Akkana Peck
> Thomas Jollans :
> > 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 <<00 68 65 6c 6c 6f 0a>hello.<
>06

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
>hello.<   ^J 
  20   3e   68   65   6c   6c   6f   2e   3ca 
  32   62  104  101  108  108  111   46   60   10 

https://github.com/akkana/scripts/blob/master/bdump

...Akkana
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Writing bytes to stdout reverses the bytes

2018-08-20 Thread Grant Edwards
On 2018-08-20, Chris Angelico  wrote:
> On Mon, Aug 20, 2018 at 12:01 PM, Grant Edwards
>> What do you mean "run it as hd"?
>>
[... Calling via 'hd' alias makes no difference ...]

> Your system is different from mine, then.

No doubt. :)

> rosuav@sikorsky:~$ ls -l $(which hd)
> lrwxrwxrwx 1 root root 7 Apr 12  2017 /usr/bin/hd -> hexdump
> rosuav@sikorsky:~$ ls -l $(which hexdump)
> -rwxr-xr-x 1 root root 27248 Apr 12  2017 /usr/bin/hexdump
>
> In 'man hexdump', the -C option says:
>  -C  Canonical hex+ASCII display.  Display the input offset in hexa‐
>  decimal, followed by sixteen space-separated, two column, hexa‐
>  decimal bytes, followed by the same sixteen bytes in %_p format
>  enclosed in ``|'' characters.
>
>  Calling the command hd implies this option.

$ hexdump --version
hexdump from util-linux 2.32

In 'man hexdump', the -C option says the exact same thing, but without
that last sentence.  The 'hd' alias implying -C seems to be a BSD
thing.  It's not mentioned in the official Linux hexdump man page:

  
https://git.kernel.org/pub/scm/utils/util-linux/util-linux.git/tree/text-utils/hexdump.1

But this page does mention 'hd':

  https://www.freebsd.org/cgi/man.cgi?query=hexdump=1

-- 
Grant Edwards   grant.b.edwardsYow! I just forgot my whole
  at   philosophy of life!!!
  gmail.com

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Writing bytes to stdout reverses the bytes

2018-08-20 Thread Marko Rauhamaa
Thomas Jollans :

> On 2018-08-20 04:22, Chris Angelico wrote:
>> On Mon, Aug 20, 2018 at 12:01 PM, Grant Edwards
>>  wrote:
>>> On 2018-08-20, Ben Bacarisse  wrote:
 It is if you run it as hd.
>>> What do you mean "run it as hd"?
>>> I don't have an "hd" in my path.
>> Your system is different from mine, then.
> Wonderful. Now why don't we all forget about hexdump and use xxd? ;-)
>
> FWIW, I have an Ubuntu system with hd, and an SL7 system without.

Fedora:

   $ xxd
   bash: xxd: command not found
   $ hd
   bash: hd: command not found
   $ od -Ax -tx1z -v <

Re: Writing bytes to stdout reverses the bytes

2018-08-20 Thread Thomas Jollans
On 2018-08-20 04:22, Chris Angelico wrote:
> On Mon, Aug 20, 2018 at 12:01 PM, Grant Edwards
>  wrote:
>> On 2018-08-20, Ben Bacarisse  wrote:
>>> It is if you run it as hd.
>>
>> What do you mean "run it as hd"?
>>
>> I don't have an "hd" in my path.
> 
> Your system is different from mine, then.

Wonderful. Now why don't we all forget about hexdump and use xxd? ;-)


FWIW, I have an Ubuntu system with hd, and an SL7 system without.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Writing bytes to stdout reverses the bytes

2018-08-19 Thread Chris Angelico
On Mon, Aug 20, 2018 at 12:01 PM, Grant Edwards
 wrote:
> On 2018-08-20, Ben Bacarisse  wrote:
>> Grant Edwards  writes:
>>
>>> On 2018-08-20, Steven D'Aprano  wrote:
 On Mon, 20 Aug 2018 00:31:35 +, Steven D'Aprano wrote:

> When I write bytes to stdout, why are they reversed?

 Answer: they aren't, use hexdump -C.
>>>
>>> One might think that dumping out bytes in the correct order ought to
>>> be the default format for hexdump.
>>
>> It is if you run it as hd.
>
> What do you mean "run it as hd"?
>
> I don't see any mention of it the hexdump manpage, and I don't have an
> "hd" in my path.  Some commands alter their behavior based on argv[0],
> so I created an alias named "hd" that points to hexdump, but that
> doesn't do anything any different than "hexdump":
>
> $ which hexdump
> /usr/bin/hexdump
>
> $ ls -l $(which hd)
> lrwxrwxrwx 1 grante users 16 Aug 19 21:00 /home/grante/bin/hd -> 
> /usr/bin/hexdump
>
> $ hd .bashrc | head -n2
> 000 2023 652f 6374 732f 656b 2f6c 622e 7361
> 010 7268 0a63 0a23 2023 6854 7369 6620 6c69
>
> $ hexdump .bashrc | head -n2
> 000 2023 652f 6374 732f 656b 2f6c 622e 7361
> 010 7268 0a63 0a23 2023 6854 7369 6620 6c69
>

Your system is different from mine, then.

rosuav@sikorsky:~$ ls -l $(which hd)
lrwxrwxrwx 1 root root 7 Apr 12  2017 /usr/bin/hd -> hexdump
rosuav@sikorsky:~$ ls -l $(which hexdump)
-rwxr-xr-x 1 root root 27248 Apr 12  2017 /usr/bin/hexdump

In 'man hexdump', the -C option says:
 -C  Canonical hex+ASCII display.  Display the input offset in hexa‐
 decimal, followed by sixteen space-separated, two column, hexa‐
 decimal bytes, followed by the same sixteen bytes in %_p format
 enclosed in ``|'' characters.

 Calling the command hd implies this option.

I'm on Debian GNU/Linux, and the 'hexdump' command comes from a
package called bsdmainutils. What are you on, and where is your
hexdump from?

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Writing bytes to stdout reverses the bytes

2018-08-19 Thread Grant Edwards
On 2018-08-20, Ben Bacarisse  wrote:
> Grant Edwards  writes:
>
>> On 2018-08-20, Steven D'Aprano  wrote:
>>> On Mon, 20 Aug 2018 00:31:35 +, Steven D'Aprano wrote:
>>>
 When I write bytes to stdout, why are they reversed?
>>>
>>> Answer: they aren't, use hexdump -C.
>>
>> One might think that dumping out bytes in the correct order ought to
>> be the default format for hexdump.
>
> It is if you run it as hd.

What do you mean "run it as hd"?

I don't see any mention of it the hexdump manpage, and I don't have an
"hd" in my path.  Some commands alter their behavior based on argv[0],
so I created an alias named "hd" that points to hexdump, but that
doesn't do anything any different than "hexdump":

$ which hexdump
/usr/bin/hexdump

$ ls -l $(which hd)
lrwxrwxrwx 1 grante users 16 Aug 19 21:00 /home/grante/bin/hd -> 
/usr/bin/hexdump

$ hd .bashrc | head -n2
000 2023 652f 6374 732f 656b 2f6c 622e 7361
010 7268 0a63 0a23 2023 6854 7369 6620 6c69

$ hexdump .bashrc | head -n2
000 2023 652f 6374 732f 656b 2f6c 622e 7361
010 7268 0a63 0a23 2023 6854 7369 6620 6c69



-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Writing bytes to stdout reverses the bytes

2018-08-19 Thread Chris Angelico
On Mon, Aug 20, 2018 at 11:31 AM, Grant Edwards
 wrote:
> On 2018-08-20, Chris Angelico  wrote:
>> On Mon, Aug 20, 2018 at 11:12 AM, Grant Edwards
>> wrote:
>>> On 2018-08-20, Steven D'Aprano  wrote:
 On Mon, 20 Aug 2018 00:31:35 +, Steven D'Aprano wrote:

> When I write bytes to stdout, why are they reversed?

 Answer: they aren't, use hexdump -C.
>>>
>>> One might think that dumping out bytes in the correct order ought to
>>> be the default format for hexdump.  Dog only know why the actual
>>> default format was chosen.  If it was 16-bit values in _octal_ you
>>> could at least blame the PDP-11 heritage of Unix...
>>>
>>
>> It's dumping sixteen-bit units in correct order.
>
> I know.  What I don't understand is is why 16-bit units in hex is the
> default.  8-bits make sense.  32-bits makes sense.  16-bits in octal
> makes sense (at least to those of us who first used Unix on a PDP-11).
>

Ah, I see what you mean. TBH, I no longer am surprised at weird
command defaults; many of them carry long history and sometimes
arbitrariness, maintained forever for backward compatibility. In this
specific case, I just use 'hd' instead of 'hexdump' - the defaults are
more to my liking, and it's shorter to boot.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Writing bytes to stdout reverses the bytes

2018-08-19 Thread Ben Bacarisse
Grant Edwards  writes:

> On 2018-08-20, Steven D'Aprano  wrote:
>> On Mon, 20 Aug 2018 00:31:35 +, Steven D'Aprano wrote:
>>
>>> When I write bytes to stdout, why are they reversed?
>>
>> Answer: they aren't, use hexdump -C.
>
> One might think that dumping out bytes in the correct order ought to
> be the default format for hexdump.

It is if you run it as hd.

-- 
Ben.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Writing bytes to stdout reverses the bytes

2018-08-19 Thread Grant Edwards
On 2018-08-20, Chris Angelico  wrote:
> On Mon, Aug 20, 2018 at 11:12 AM, Grant Edwards
> wrote:
>> On 2018-08-20, Steven D'Aprano  wrote:
>>> On Mon, 20 Aug 2018 00:31:35 +, Steven D'Aprano wrote:
>>>
 When I write bytes to stdout, why are they reversed?
>>>
>>> Answer: they aren't, use hexdump -C.
>>
>> One might think that dumping out bytes in the correct order ought to
>> be the default format for hexdump.  Dog only know why the actual
>> default format was chosen.  If it was 16-bit values in _octal_ you
>> could at least blame the PDP-11 heritage of Unix...
>>
>
> It's dumping sixteen-bit units in correct order.

I know.  What I don't understand is is why 16-bit units in hex is the
default.  8-bits make sense.  32-bits makes sense.  16-bits in octal
makes sense (at least to those of us who first used Unix on a PDP-11).

-- 
Grant



-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Writing bytes to stdout reverses the bytes

2018-08-19 Thread Chris Angelico
On Mon, Aug 20, 2018 at 11:12 AM, Grant Edwards
 wrote:
> On 2018-08-20, Steven D'Aprano  wrote:
>> On Mon, 20 Aug 2018 00:31:35 +, Steven D'Aprano wrote:
>>
>>> When I write bytes to stdout, why are they reversed?
>>
>> Answer: they aren't, use hexdump -C.
>
> One might think that dumping out bytes in the correct order ought to
> be the default format for hexdump.  Dog only know why the actual
> default format was chosen.  If it was 16-bit values in _octal_ you
> could at least blame the PDP-11 heritage of Unix...
>

It's dumping sixteen-bit units in correct order.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Writing bytes to stdout reverses the bytes

2018-08-19 Thread Grant Edwards
On 2018-08-20, Steven D'Aprano  wrote:
> On Mon, 20 Aug 2018 00:31:35 +, Steven D'Aprano wrote:
>
>> When I write bytes to stdout, why are they reversed?
>
> Answer: they aren't, use hexdump -C.

One might think that dumping out bytes in the correct order ought to
be the default format for hexdump.  Dog only know why the actual
default format was chosen.  If it was 16-bit values in _octal_ you
could at least blame the PDP-11 heritage of Unix...




-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Writing bytes to stdout reverses the bytes

2018-08-19 Thread Steven D'Aprano
On Mon, 20 Aug 2018 00:31:35 +, Steven D'Aprano wrote:

> When I write bytes to stdout, why are they reversed?

Answer: they aren't, use hexdump -C.

Thanks to all replies!


-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Writing bytes to stdout reverses the bytes

2018-08-19 Thread Cameron Simpson

On 20Aug2018 00:31, Steven D'Aprano  
wrote:

When I write bytes to stdout, why are they reversed?

[steve@ando ~]$ python2.7 -c "print('\xfd\x84\x04\x08')" | hexdump
000 84fd 0804 000a
005

[steve@ando ~]$ python3.5 -c "import sys; sys.stdout.buffer.write(b'\xfd
\x84\x04\x08\n')" | hexdump
000 84fd 0804 000a
005


They're not. Hexdump is presenting your bytes as little endian 2 byte words. So 
your leading 0xfd is the _low_ bytes of the 2 bytes word.


Try "od -c" instead.

Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Writing bytes to stdout reverses the bytes

2018-08-19 Thread Chris Angelico
On Mon, Aug 20, 2018 at 10:31 AM, Steven D'Aprano
 wrote:
> When I write bytes to stdout, why are they reversed?
>
> [steve@ando ~]$ python2.7 -c "print('\xfd\x84\x04\x08')" | hexdump
> 000 84fd 0804 000a
> 005
>
> [steve@ando ~]$ python3.5 -c "import sys; sys.stdout.buffer.write(b'\xfd
> \x84\x04\x08\n')" | hexdump
> 000 84fd 0804 000a
> 005

They're not. They're being printed out as individual bytes, but then
the hexdump command is looking at little-endian sixteen-bit units. Try
the 'hd' command instead, or use 'hexdump -C'.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Writing bytes to stdout reverses the bytes

2018-08-19 Thread Grant Edwards
On 2018-08-20, Steven D'Aprano  wrote:

> When I write bytes to stdout, why are they reversed?
>
> [steve@ando ~]$ python2.7 -c "print('\xfd\x84\x04\x08')" | hexdump
> 000 84fd 0804 000a
> 005

They aren't.  You're being fooled by the default output format of
hexdump.  By default, it displays data as 16-byte integers in
little-endian.  If you want to look at bytes, do this:

$ python2.7 -c "print('\xfd\x84\x04\x08')" | hexdump -C
  fd 84 04 08 0a|.|
0005


-- 
https://mail.python.org/mailman/listinfo/python-list