InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Viewing Output in Hex in bash
Bash

Viewing Output in Hex in bash

InfinityCoder April 4, 2017

You need to see output in hex mode to verify that a certain whitespace or unprintable character is as expected.

Pipe the output though hexdump using the -C option for canonical output:

1
2
3
4
$ hexdump -C filename
00000000 4c 69 6e 65 20 31 0a 4c 69 6e 65 20 32 0a 0a 4c |Line 1.Line 2..L|
00000010 69 6e 65 20 34 0a 4c 69 6e 65 20 35 0a 0a       |ine 4.Line 5..|
0000001e

For example, nl uses spaces (ASCII 20), then the line number, then a tab (ASCII 09)
in its output:

1
2
3
4
5
6
7
$ nl -ba filename | hexdump -C
00000000 20 20 20 20 20 31 09 4c 69 6e 65 20 31 0a 20 20 |    1.Line 1.  |
00000010 20 20 20 32 09 4c 69 6e 65 20 32 0a 20 20 20 20 |   2.Line 2.   |
00000020 20 33 09 0a 20 20 20 20 20 34 09 4c 69 6e 65 20 | 3..    4.Line |
00000030 34 0a 20 20 20 20 20 35 09 4c 69 6e 65 20 35 0a |4.    5.Line 5.|
00000040 20 20 20 20 20 36 09 0a                         |      6..|
00000048

hexdump is a BSD utility that also comes with many Linux distributions.

Other systems, notably Solaris, do not have it by default.

You can use the octal dump command od, but it’s a lot harder to read:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ nl -ba filename | od -x
0000000 2020 2020 3120 4c09 6e69 2065 0a31 2020
0000020 2020 3220 4c09 6e69 2065 0a32 2020 2020
0000040 3320 0a09 2020 2020 3420 4c09 6e69 2065
0000060 0a34 2020 2020 3520 4c09 6e69 2065 0a35
0000100 2020 2020 3620 0a09
0000110
 
 
$ nl -ba filename | od -tx1
0000000 20 20 20 20 20 31 09 4c 69 6e 65 20 31 0a 20 20
0000020 20 20 20 32 09 4c 69 6e 65 20 32 0a 20 20 20 20
0000040 20 33 09 0a 20 20 20 20 20 34 09 4c 69 6e 65 20
0000060 34 0a 20 20 20 20 20 35 09 4c 69 6e 65 20 35 0a
0000100 20 20 20 20 20 36 09 0a
0000110

There is also a simple Perl script available at http://www.khngai.com/perl/bin/
hexdump.txt that might work:

1
2
3
4
5
$ ./hexdump.pl filename
 
       /0 /1 /2 /3 /4 /5 /6 /7 /8 /9/ A /B /C /D /E /F   0123456789ABCDEF
0000 : 4C 69 6E 65 20 31 0A 4C 69 6E 65 20 32 0A 0A 4C   Line 1.Line 2..L
0010 : 69 6E 65 20 34 0A 4C 69 6E 65 20 35 0A 0A         ine 4.Line 5..

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Using Parameters in a Shell Script in bash
You also want users to be able to invoke your …

Using Parameters in a Shell Script in bash

Setting Permissions in bash
You want to set permissions in a secure manner. If …

Setting Permissions in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

    Categories

    • Bash
    • PHP
    • Python
    • Uncategorized

    InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

    About Us

    Start learning your desired programming language with InfinityQuest.com.

    On our website you can access any tutorial that you want with video and code examples.

    We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

    Popular Tags

    binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

    Archives

    • June 2017
    • April 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    Copyright © 2019 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
    Programming Tutorials | Sitemap