Skip to content

into binary behavior is surprising #3414

Closed

Description

@agateau

Describe the bug
I wanted to dump the first bytes of a PNG file using cat myimage.png | into binary -b 100 but got a table instead of a binary view.

To Reproduce
Steps to reproduce the behavior:

  1. cat myimage.png | into binary -b 100

Expected behavior

I expected an output like this:

Length: 140 (0x8c) bytes | printable whitespace ascii_other non_ascii
00000000:   ef bf bd 50  4e 47 0d 0a  1a 0a 00 00  00 0d 49 48   ×××PNG__•_000_IH
00000010:   44 52 00 00  07 76 00 00  04 18 08 06  00 00 00 ef   DR00•v00••••000×
00000020:   bf bd 2d 38  ef bf bd 00  00 00 09 70  48 59 73 00   ××-8×××000_pHYs0
00000030:   00 0e ef bf  bd 00 00 0e  ef bf bd 01  ef bf bd 2b   0•×××00•××ו×××+
00000040:   0e 1b 00 00  20 00 49 44  41 54 78 ef  bf bd ef bf   ••00 0IDATx×××××
00000050:   bd ef bf bd  79 7c 14 ef  bf bd ef bf  bd ef bf bd   ××××y|•×××××××××
00000060:   ef bf bd ef  bf bd 77 76  73 10 6e 0f  50 7b ef bf   ××××××wvs•n•P{××
00000070:   bd 55 10 ef  bf bd 79 ef  bf bd 1c 49  ef bf bd 20   ×U•×××y××וI××× 
00000080:   ef bf bd 72  58 2b 1a ef  bf bd d5 aa                ×××rX+•×××××

(I got this output using cat img.png | into binary -b 100 | nth 0 as suggested on Discord)

Configuration (please complete the following information):

Run version | pivot and paste the output to show OS, features, etc.

Column0 Column1
version 0.31.0
short_commit b93b80c
commit_hash b93b80c
commit_date 2021-05-11 07:59:24
build_os linux-x86_64
rust_version rustc 1.52.1 (9bc8c42bb 2021-05-09)
rust_channel stable-x86_64-unknown-linux-gnu (default)
cargo_version cargo 1.52.0 (69767412a 2021-04-21)
pkg_version 0.31.0
build_time 2021-05-11 10:07:34
build_rust_channel release
features clipboard-cli, ctrlc, default, directories, dirs, ptree, rustyline, term, trash, uuid, which, zip

Activity

  1. sophiajt commented on May 12, 2021

    @sophiajt
    Contributor

    tagging @fdncred, who worked on this

  2. fdncred commented on May 12, 2021

    @fdncred
    Contributor

    it's because you're using cat instead of open. I thought cat was meant for text files. I've never tried to use it on binary files like a png file.
    image

    I can't even do your workaround without it horking my screen and giving an error with cat, but i'm on windows so i'm sure things are handled differently there.

  3. sophiajt commented on May 12, 2021

    @sophiajt
    Contributor

    @fdncred - I'm seeing the same issue with open. It seems to be related to how big the binary is. If it's larger than 8k (our current packet size), then into binary will give back rows of binary rather than a hex output.

  4. agateau commented on May 13, 2021

    @agateau
    Author

    If I am not mistaken, cat does not care about the file content, it just sends bytes. For example what I tried to achieve can be done like this in a "traditional" unix shell:

    cat img.png | head -c 100 | xxd
    

    On the other hand, if the right way to do this with Nushell is through open that's OK too. Changing tools can require changing habits, but open produced strange results: It worked well with a 10.8MB jpeg but not with a 59.8MB mp4. Here is a transcript:

    $ ls
    ───┬──────────────┬──────┬─────────┬────────────
     # │     name     │ type │  size   │  modified  
    ───┼──────────────┼──────┼─────────┼────────────
     0 │ DSC_0907.JPG │ File │ 10.8 MB │ 5 mins ago 
     1 │ video.mp4    │ File │ 59.8 MB │ 5 mins ago 
    ───┴──────────────┴──────┴─────────┴────────────
    
    $ open DSC_0907.JPG | into binary -b 100
    Length: 115 (0x73) bytes | printable whitespace ascii_other non_ascii
    00000000:   ef bf bd ef  bf bd ef bf  bd ef bf bd  1b 45 78 69   ×××××××××××וExi
    00000010:   66 00 00 49  49 2a 00 08  00 00 00 0b  00 0f 01 02   f00II*0•000•0•••
    00000020:   00 05 00 00  00 ef bf bd  00 00 00 10  01 02 00 06   0•000×××000•••0•
    00000030:   00 00 00 ef  bf bd 00 00  00 12 01 03  00 01 00 00   000×××000•••0•00
    00000040:   00 01 00 00  00 1a 01 05  00 01 00 00  00 ef bf bd   0•000•••0•000×××
    00000050:   00 00 00 1b  01 05 00 01  00 00 00 ef  bf bd 00 00   000•••0•000×××00
    00000060:   00 28 01 03  00 01 00 00  00 02 00 00  00 31 01 02   0(••0•000•0001••
    00000070:   00 15 00                                             0•0
    
    $ open video.mp4 | into binary -b 100
    ───┬─────────────────────
     0 │ <binary: 100 bytes> 
     1 │ <binary: 100 bytes> 
     2 │ <binary: 100 bytes> 
     3 │ <binary: 100 bytes> 
     4 │ <binary: 100 bytes> 
     5 │ <binary: 100 bytes> 
     6 │ <binary: 100 bytes> 
     7 │ <binary: 100 bytes> 
     8 │ <binary: 100 bytes> 
     9 │ <binary: 100 bytes> 
    (...)
    
  5. sophiajt commented on May 13, 2021

    @sophiajt
    Contributor

    Seems like into binary is applying the same thing to every row instead of counting the total bytes and returning that many.

  6. github-actions commented on Jun 24, 2021

    @github-actions

    This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

  7. github-actions commented on Jul 8, 2021

    @github-actions

    This issue has been marked stale for more than 10 days without activity. Closing this issue, but if you find that the issue is still valid, please reopen.

  8. fdncred commented on Jul 8, 2021

    @fdncred
    Contributor

    I may work on this.

  9. reopened this on Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions