Friday, May 20, 2011

Display Binary Contents Of A File

Use the od command to dump a file to the console as binary (ascii-hex). This even works on the MAC. It's useful when you're having problems with control characters like line endings.

$ od -t x1 test1.sh
0000000    23  21  2f  62  69  6e  2f  6b  73  68  20  2d  65  70  75  0a
0000020    0a  65  63  68  6f  20  22  46  69  6e  69  73  68  65  64  21
0000040    22  0a  0a                                                   
0000043
$ od -t x1 test2.sh
0000000    23  21  2f  62  69  6e  2f  6b  73  68  20  2d  65  70  75  0d
0000020    0a  0d  0a  65  63  68  6f  20  22  46  69  6e  69  73  68  65
0000040    64  21  22  0d  0a                                           
0000045

If you want to see it as ASCII, here's a converter

{ "loggedin": false, "owner": false, "avatar": "", "render": "nothing", "trackingID": "UA-36983794-1", "description": "How to display the contents of a file as hex in a linux console.", "page": { "blogIds": [ 52 ] }, "domain": "holtstrom.com", "base": "\/michael", "url": "https:\/\/holtstrom.com\/michael\/", "frameworkFiles": "https:\/\/holtstrom.com\/michael\/_framework\/_files.4\/", "commonFiles": "https:\/\/holtstrom.com\/michael\/_common\/_files.3\/", "mediaFiles": "https:\/\/holtstrom.com\/michael\/media\/_files.3\/", "tmdbUrl": "http:\/\/www.themoviedb.org\/", "tmdbPoster": "http:\/\/image.tmdb.org\/t\/p\/w342" }