{"id":1269,"date":"2009-04-20T05:13:55","date_gmt":"2009-04-20T12:13:55","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=1269"},"modified":"2009-04-20T05:13:55","modified_gmt":"2009-04-20T12:13:55","slug":"how-to-make-a-quick-dirty-hexviewer-updated","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/how-to-make-a-quick-dirty-hexviewer-updated\/","title":{"rendered":"How to make a Quick &#038; Dirty HexViewer &#8211; Updated"},"content":{"rendered":"<p>After I received comments from <a rel=\"nofollow\" href=\"http:\/\/www.flickr.com\/photos\/gubatron\/3452721506\/\">ispak on Flickr<\/a> I made a few fixes.<\/p>\n<p>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&#8217;t catch any exception :p<\/p>\n<p>So now I read 16 byte chunks, and I also take care of the file ending. The previous version used to print the file ending with a bunch of null bytes. Now it stops reading at the end, and formats the output accordingly.<\/p>\n<p>Here&#8217;s the new source:<\/p>\n<p>[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        try {<br \/>\n            File f = new File(filePath);<br \/>\n            BufferedInputStream bis =<br \/>\n                new BufferedInputStream(new FileInputStream(f));<\/p>\n<p>            byte[] chunk = null;<br \/>\n            int readStatus = 0;<br \/>\n            while (true) {<br \/>\n                chunk = new byte[16];<br \/>\n                readStatus = bis.read(chunk, 0, 16);<br \/>\n                char[] line = new char[16];<\/p>\n<p>                if (readStatus == -1)<br \/>\n                    break;<\/p>\n<p>                for (byte i=0; i &lt; readStatus; i++) {<br \/>\n                    int readByte = (chunk[i] &lt; 0) ? (-1 * (int) chunk[i]) : chunk[i];<br \/>\n                    String paddingZero = (readByte &lt; 16) ? &quot;0&quot; : &quot;&quot;;<br \/>\n                    System.out.print(paddingZero + Integer.toHexString(readByte).toUpperCase() + &quot; &quot;);<br \/>\n                    line[i] = (readByte &gt;= 33 &amp;&amp; readByte &lt;= 126) ? (char) readByte : &#8216;.&#8217;;<br \/>\n                }<\/p>\n<p>                \/\/We add some padding to print the text line right below the one above.<br \/>\n                String padding = new String();<br \/>\n                if (readStatus &lt; 16) {<br \/>\n                    for (byte i=0; i &lt; 16-readStatus; i++) {<br \/>\n                        padding += &quot;   &quot;;<br \/>\n                    }<br \/>\n                }<\/p>\n<p>                System.out.println(padding + 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><a href=\"http:\/\/www.flickr.com\/photos\/gubatron\/3458529439\/\" title=\"HexViewer - r2 by Gubatron, on Flickr\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3486\/3458529439_31357e0afe_o.png?resize=570%2C641\" width=\"570\" height=\"641\" alt=\"HexViewer - r2\" \/><\/a><\/p>\n<p>And see how it now handles file endings when the file size is not a multiple of 16 :p<\/p>\n<p><a href=\"http:\/\/www.flickr.com\/photos\/gubatron\/3459348222\/\" title=\"Picture 2 by Gubatron, on Flickr\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3578\/3459348222_ee4b427d5e_o.png?resize=464%2C91\" width=\"464\" height=\"91\" alt=\"Picture 2\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;t catch any exception :p So now I read 16 byte chunks, and I also take care of the [&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,43],"tags":[1437,584],"class_list":["post-1269","post","type-post","status-publish","format-standard","hentry","category-code","category-linux","tag-code","tag-java-hex-hexviewer-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-kt","jetpack-related-posts":[{"id":1928,"url":"https:\/\/www.gubatron.com\/blog\/droid-vs-nexus-1-who-can-calculate-md5-faster\/","url_meta":{"origin":1269,"position":0},"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":1252,"url":"https:\/\/www.gubatron.com\/blog\/how-to-make-your-own-quick-dirty-hex-file-viewer-in-java\/","url_meta":{"origin":1269,"position":1},"title":"How to make your own Quick &#038; Dirty Hex File Viewer in Java","author":"gubatron","date":"April 18, 2009","format":false,"excerpt":"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's a quick & dirty\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"Same file on HexEdit","src":"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3634\/3452718760_995edb5218_o.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3634\/3452718760_995edb5218_o.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/farm4.static.flickr.com\/3634\/3452718760_995edb5218_o.png?resize=525%2C300 1.5x"},"classes":[]},{"id":2063,"url":"https:\/\/www.gubatron.com\/blog\/android-programming-how-to-obtain-the-wifis-corresponding-networkinterface\/","url_meta":{"origin":1269,"position":2},"title":"Android: How to obtain the WiFi&#8217;s corresponding NetworkInterface","author":"gubatron","date":"September 19, 2010","format":false,"excerpt":"Let's say for some odd reason in the world you do need to get the corresponding NetworkInterface object of the Wifi on your android, in my case I needed to have my WiFi device send multicast packets, and I wanted my MulticastSocket to only send packets through the WiFi device\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":4000,"url":"https:\/\/www.gubatron.com\/blog\/what-is-the-rust-equivalent-to-javas-printwriter\/","url_meta":{"origin":1269,"position":3},"title":"What is the Rust equivalent to Java&#8217;s PrintWriter?","author":"gubatron","date":"December 21, 2022","format":false,"excerpt":"In Rust, the equivalent of Java's PrintWriter is the std::io::Write trait, which is implemented by a number of types that can be used to write data to an output stream, such as a file or a network socket. To use Write to write text to an output stream, you can\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2022\/12\/progress_image_100_7fdc7b72-6c19-42f5-affe-d055d02d6f8e.webp?fit=1024%2C1024&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2022\/12\/progress_image_100_7fdc7b72-6c19-42f5-affe-d055d02d6f8e.webp?fit=1024%2C1024&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2022\/12\/progress_image_100_7fdc7b72-6c19-42f5-affe-d055d02d6f8e.webp?fit=1024%2C1024&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2022\/12\/progress_image_100_7fdc7b72-6c19-42f5-affe-d055d02d6f8e.webp?fit=1024%2C1024&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":3616,"url":"https:\/\/www.gubatron.com\/blog\/codejava-argb_8888-pixel-abstraction\/","url_meta":{"origin":1269,"position":4},"title":"[CODE\/JAVA] ARGB_8888 Pixel Abstraction","author":"gubatron","date":"December 4, 2016","format":false,"excerpt":"This is one of the ways you can decode Pixel colors out of the integers you get from Android Pixels. ARGB_8888, stands for Alpha, Reg, Green, Blue. The 8's stand for the number of bits per channel. In Android, signed int's are used to represent pixel's alpha\/color information. Since Android's\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":980,"url":"https:\/\/www.gubatron.com\/blog\/javareflection-notes-invoking-a-static-main-method-from-a-dinamically-loaded-class\/","url_meta":{"origin":1269,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/1269","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=1269"}],"version-history":[{"count":0,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/1269\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=1269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=1269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=1269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}