{"id":1252,"date":"2009-04-18T06:04:33","date_gmt":"2009-04-18T13:04:33","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=1252"},"modified":"2009-04-18T06:04:33","modified_gmt":"2009-04-18T13:04:33","slug":"how-to-make-your-own-quick-dirty-hex-file-viewer-in-java","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/how-to-make-your-own-quick-dirty-hex-file-viewer-in-java\/","title":{"rendered":"How to make your own Quick &#038; Dirty Hex File Viewer in Java"},"content":{"rendered":"<p><strong>Update:<\/strong> You might want to read this <a href=\"http:\/\/www.gubatron.com\/blog\/2009\/04\/20\/how-to-make-a-quick-dirty-hexviewer-updated\/\">new version of the code <\/a> instead. Thanks ispak<\/p>\n<p>I was playing with a hex editor recently and then I thought it would be pretty easy to make a program to output what you see on a text editor. Here&#8217;s a quick &#038; dirty Hex Visor I wrote in like 5 minutes with Java. It shows 15 bytes per line, and on the right side it prints all the visible characters of the ascii table, the non-visible ones are replaced with &#8220;.&#8221;<\/p>\n<p>Enjoy:<br \/>\n[java]<br \/>\n\/\/HexViewer.java<br \/>\nimport java.io.*;<\/p>\n<p>public final class HexViewer {<br \/>\n    public final static void printFile(String filePath) {<br \/>\n        File f;<br \/>\n        try {<br \/>\n            f = new File(filePath);<br \/>\n        } catch (Exception e) {<br \/>\n            return;<br \/>\n        }<\/p>\n<p>        try {<\/p>\n<p>            FileInputStream fis = new FileInputStream(f);<br \/>\n            while (fis.available() &gt; 0) {<br \/>\n                char[] line = new char[16];<br \/>\n                for (int i=0; i &lt; 16; i++) {<br \/>\n                    int readByte = fis.read();<br \/>\n                    String paddingZero = (readByte &lt; 16) ? &quot;0&quot; : &quot;&quot;;<br \/>\n                    System.out.print(paddingZero + Integer.toHexString(readByte) + &quot; &quot;);<br \/>\n                    line[i] = (readByte &gt;= 33 &amp;&amp; readByte &lt;= 126) ? (char) readByte : &#8216;.&#8217;;<br \/>\n                }<br \/>\n                System.out.println(new String(line));<br \/>\n            }<br \/>\n        } catch (Exception e1) { e1.printStackTrace(); }<br \/>\n    }<\/p>\n<p>    public final static void main(String[] args) {<br \/>\n        if (args.length == 0)<br \/>\n            return;<\/p>\n<p>        printFile(args[0]);<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Usage:<br \/>\n[bash]java HexViewer &amp;lt;path to file&gt; | less[\/bash]<\/p>\n<p>[bash]java HexViewer Desktop\/Puppet.wmv | less<\/p>\n<p>30 26 b2 75 8e 66 cf 11 a6 d9 00 aa 00 62 ce 6c 0&amp;.u.f&#8230;&#8230;.b.l<br \/>\n74 14 00 00 00 00 00 00 07 00 00 00 01 02 a1 dc t&#8230;&#8230;&#8230;&#8230;&#8230;<br \/>\nab 8c 47 a9 cf 11 8e e4 00 c0 0c 20 53 65 68 00 ..G&#8230;&#8230;&#8230;Seh.<br \/>\n00 00 00 00 00 00 f4 10 68 f9 49 76 2e 43 b5 9f &#8230;&#8230;..h.Iv.C..<br \/>\n09 0a 2d 19 45 7c 32 3a 24 00 00 00 00 00 70 ee ..-.E|2:$&#8230;..p.<br \/>\n14 06 fb da c7 01 28 01 00 00 00 00 00 00 00 8c &#8230;&#8230;(&#8230;&#8230;&#8230;<br \/>\nbb 53 00 00 00 00 10 88 dd 52 00 00 00 00 b8 0b .S&#8230;&#8230;.R&#8230;&#8230;<br \/>\n00 00 00 00 00 00 02 00 00 00 40 1f 00 00 40 1f &#8230;&#8230;&#8230;.@&#8230;@.<br \/>\n00 00 90 50 02 00 b5 03 bf 5f 2e a9 cf 11 8e e3 &#8230;P&#8230;.._&#8230;&#8230;<br \/>\n00 c0 0c 20 53 65 61 10 00 00 00 00 00 00 11 d2 &#8230;.Sea&#8230;&#8230;&#8230;<br \/>\nd3 ab ba a9 cf 11 8e e6 00 c0 0c 20 53 65 06 00 &#8230;&#8230;&#8230;&#8230;Se..<br \/>\n33 10 00 00 a9 46 43 7c e0 ef fc 4b b2 29 39 3e 3&#8230;.FC|&#8230;K.)9&gt;<br \/>\nde 41 5c 85 27 00 00 00 00 00 00 00 01 00 0c 65 .A.&#8217;&#8230;&#8230;&#8230;.e<br \/>\n00 6e 00 2d 00 61 00 75 00 00 00 5d 8b f1 26 84 .n.-.a.u&#8230;]..&amp;.<br \/>\n45 ec 47 9f 5f 0e 65 1f 04 52 c9 1a 00 00 00 00 E.G._.e..R&#8230;&#8230;<br \/>\n00 00 00 02 01 ea cb f8 c5 af 5b 77 48 84 67 aa &#8230;&#8230;&#8230;.[wH.g.<br \/>\n8c 44 fa 4c ca 62 01 00 00 00 00 00 00 06 00 00 .D.L.b&#8230;&#8230;&#8230;.<br \/>\n00 01 00 0c 00 02 00 02 00 00 00 49 00 73 00 56 &#8230;&#8230;&#8230;..I.s.V<br \/>\n00 42 00 52 00 00 00 00 00 00 00 01 00 34 00 00 .B.R&#8230;&#8230;&#8230;4..<br \/>\n00 06 00 00 00 44 00 65 00 76 00 69 00 63 00 65 &#8230;..D.e.v.i.c.e<br \/>\n00 43 00 6f 00 6e 00 66 00 6f 00 72 00 6d 00 61 .C.o.n.f.o.r.m.a<br \/>\n00 6e 00 63 00 65 00 54 00 65 00 6d 00 70 00 6c .n.c.e.T.e.m.p.l<br \/>\n00 61 00 74 00 65 00 00 00 4c 00 31 00 00 00 00 .a.t.e&#8230;L.1&#8230;.<br \/>\n00 02 00 0c 00 02 00 02 00 00 00 49 00 73 00 56 &#8230;&#8230;&#8230;..I.s.V<br \/>\n00 42 00 52 00 00 00 01 00 00 00 02 00 34 00 00 .B.R&#8230;&#8230;&#8230;4..<br \/>\n00 0c 00 00 00 44 00 65 00 76 00 69 00 63 00 65 &#8230;..D.e.v.i.c.e<br \/>\n00 43 00 6f 00 6e 00 66 00 6f 00 72 00 6d 00 61 .C.o.n.f.o.r.m.a<br \/>\n00 6e 00 63 00 65 00 54 00 65 00 6d 00 70 00 6c .n.c.e.T.e.m.p.l<br \/>\n00 61 00 74 00 65 00 00 00 4d 00 50 00 40 00 4d .a.t.e&#8230;M.P.@.M<br \/>\n00 4c 00 00 00 00 00 01 00 2e 00 03 00 04 00 00 .L&#8230;&#8230;&#8230;&#8230;..<br \/>\n00 57 00 4d 00 2f 00 57 00 4d 00 41 00 44 00 52 .W.M.\/.W.M.A.D.R<br \/>\n00 43 00 50 00 65 00 61 00 6b 00 52 00 65 00 66 .C.P.e.a.k.R.e.f<br \/>\n00 65 00 72 00 65 00 6e 00 63 00 65 00 00 00 a7 .e.r.e.n.c.e&#8230;.<br \/>\n3f 00 00 00 00 01 00 34 00 03 00 04 00 00 00 57 ?&#8230;&#8230;4&#8230;&#8230;.W<br \/>\n00 4d 00 2f 00 57 00 4d 00 41 00 44 00 52 00 43 .M.\/.W.M.A.D.R.C<br \/>\n00 41 00 76 00 65 00 72 00 61 00 67 00 65 00 52 .A.v.e.r.a.g.e.R<br \/>\n00 65 00 66 00 65 00 72 00 65 00 6e 00 63 00 65 .e.f.e.r.e.n.c.e<br \/>\n00 00 00 b0 06 00 00 74 d4 06 18 df ca 09 45 a4 &#8230;&#8230;.t&#8230;&#8230;E.<br \/>\nba 9a ab cb 96 aa e8 a4 0d 00 00 00 00 00 00 00 &#8230;&#8230;&#8230;&#8230;&#8230;.<br \/>\n00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 &#8230;&#8230;&#8230;&#8230;&#8230;.<br \/>\n00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 &#8230;&#8230;&#8230;&#8230;&#8230;.<br \/>\n[\/bash]<\/p>\n<p>This how it looks on a full blown hex editor like HexEdit:<br \/>\n<a href=\"http:\/\/www.flickr.com\/photos\/gubatron\/3452718760\/\" title=\"Same file on HexEdit by Gubatron, on Flickr\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3634\/3452718760_995edb5218_o.png?resize=558%2C569\" width=\"558\" height=\"569\" alt=\"Same file on HexEdit\" \/><\/a><\/p>\n<p>The Code of HexViewer.java on emacs:<br \/>\n<a href=\"http:\/\/www.flickr.com\/photos\/gubatron\/3452721506\/\" title=\"HexViewer.java on emacs by Gubatron, on Flickr\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3379\/3452721506_abbcfd5dbf_o.png?resize=580%2C665\" width=\"580\" height=\"665\" alt=\"HexViewer.java on emacs\" \/><\/a><\/p>\n<p>Screenshot of the output:<br \/>\n<a href=\"http:\/\/www.flickr.com\/photos\/gubatron\/3452720154\/\" title=\"HexViewer in action by Gubatron, on Flickr\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3582\/3452720154_6153a04a63_o.png?resize=450%2C721\" width=\"450\" height=\"721\" alt=\"HexViewer in action\" \/><\/a><\/p>\n<p><strong>Homework<\/strong><br \/>\nHack the code so that it ouputs the first column shown on the HexEdit screenshot. That column represents the byte position of each row. It&#8217;s basically a counter incremented 16 units at the time, and shown in Hex.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update: You might want to read this new version of the code instead. Thanks ispak I was playing with a hex editor recently and then I thought it would be pretty easy to make a program to output what you see on a text editor. Here&#8217;s a quick &#038; dirty Hex Visor I wrote in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[15,30,68],"tags":[282,524,583],"class_list":["post-1252","post","type-post","status-publish","format-standard","hentry","category-code","category-geeklife","category-random-stuff","tag-coding","tag-hex-editor","tag-java"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-kc","jetpack-related-posts":[{"id":1269,"url":"https:\/\/www.gubatron.com\/blog\/how-to-make-a-quick-dirty-hexviewer-updated\/","url_meta":{"origin":1252,"position":0},"title":"How to make a Quick &#038; Dirty HexViewer &#8211; Updated","author":"gubatron","date":"April 20, 2009","format":false,"excerpt":"After I received comments from ispak on Flickr I made a few fixes. ispak pointed out that it was a bad idea reading one byte at the time, also I had a gay ass try\/catch that didn't catch any exception :p So now I read 16 byte chunks, and I\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"HexViewer - r2","src":"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3486\/3458529439_31357e0afe_o.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3486\/3458529439_31357e0afe_o.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/farm4.static.flickr.com\/3486\/3458529439_31357e0afe_o.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1928,"url":"https:\/\/www.gubatron.com\/blog\/droid-vs-nexus-1-who-can-calculate-md5-faster\/","url_meta":{"origin":1252,"position":1},"title":"Droid vs Nexus 1: Who can calculate MD5 faster?","author":"gubatron","date":"May 21, 2010","format":false,"excerpt":"Nexus 1 indeed. 17 files get their MD5 calculated on the Droid and 17 files get their MD5 hash calculated on the Nexus 1 Nexus 1 pwns. Here's the code in case you're curious. [java] public void onClick(View v) { _logTextView.setText(\"MD5 Benchmark on \" + Build.DEVICE + \"nn\"); if (GlobalVariables.APP_CONTEXT\u2026","rel":"","context":"In &quot;Android&quot;","block_context":{"text":"Android","link":"https:\/\/www.gubatron.com\/blog\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":980,"url":"https:\/\/www.gubatron.com\/blog\/javareflection-notes-invoking-a-static-main-method-from-a-dinamically-loaded-class\/","url_meta":{"origin":1252,"position":2},"title":"Java\/Reflection notes: Invoking a static main() method from a dinamically loaded class.","author":"gubatron","date":"November 22, 2008","format":false,"excerpt":"Maybe for some wild reason, your Java application will need to execute a pre launcher that won't know about the Main class it's supposed to invoke until it's being executed. For example, you have distributed your Java application but you used pack200 to compress your jars, and your new application\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2941,"url":"https:\/\/www.gubatron.com\/blog\/java-how-to-get-all-the-files-inside-a-folder-and-its-subfolders-without-recursion\/","url_meta":{"origin":1252,"position":3},"title":"java: How to get all the files inside a folder and its subfolders without recursion","author":"gubatron","date":"March 19, 2013","format":false,"excerpt":"Most programmers will do this in a recursive fashion, but doing that is putting yourself at risk of hitting a stack overflow error, and it's 20% slower (according to my tests). Here's my first implementation of a method that will return just the files (cause I didn't need the folders,\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2926,"url":"https:\/\/www.gubatron.com\/blog\/how-to-check-if-a-java-process-is-running-on-any-local-virtual-machine-programatically\/","url_meta":{"origin":1252,"position":4},"title":"How to check if a Java Process is running on any local Virtual Machine programmatically","author":"gubatron","date":"January 30, 2013","format":false,"excerpt":"Quick and dirty way to check if a Java Process is already running. Useful if you need to run cronjobs periodically and you don't know how long they might take, you can add this check at the beginning of your main, and it'll look for all the local virtual machines\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2138,"url":"https:\/\/www.gubatron.com\/blog\/java-how-to-create-dynamic-pngs-jpgs-gifs\/","url_meta":{"origin":1252,"position":5},"title":"Java: How to create dynamic PNGs, JPGs, GIFs.","author":"gubatron","date":"February 14, 2010","format":false,"excerpt":"Sometimes you need to create graphics, or compose images and have them saved as regular PNGs, JPEGs or GIFs. Here's a quick and dirty reference of how to do it with BufferedImages, Graphics2D and javax.imageio.*. Very straightforward. [java] import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO;\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/1252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=1252"}],"version-history":[{"count":0,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/1252\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=1252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=1252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=1252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}