Translate

Archive for the 'Code' Category

How to make a Quick & Dirty HexViewer – Updated

Monday, April 20th, 2009

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 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.

Here’s the new source:

//HexViewer.java
import java.io.*;

public final class HexViewer {
    public final static void printFile(String filePath) {
        try {
            File f = new File(filePath);
            BufferedInputStream bis =
                new BufferedInputStream(new FileInputStream(f));

            byte[] chunk = null;
            int readStatus = 0;
            while (true) {
                chunk = new byte[16];
                readStatus = bis.read(chunk, 0, 16);
                char[] line = new char[16];

                if (readStatus == -1)
                    break;

                for (byte i=0; i < readStatus; i++) {
                    int readByte = (chunk[i] < 0) ? (-1 * (int) chunk[i]) : chunk[i];
                    String paddingZero = (readByte < 16) ? "0" : "";
                    System.out.print(paddingZero + Integer.toHexString(readByte).toUpperCase() + " ");
                    line[i] = (readByte >= 33 && readByte <= 126) ? (char) readByte : '.';
                }

                //We add some padding to print the text line right below the one above.
                String padding = new String();
                if (readStatus < 16) {
                    for (byte i=0; i < 16-readStatus; i++) {
                        padding += "   ";
                    }
                }

                System.out.println(padding + new String(line));
            }
        } catch (Exception e1) { e1.printStackTrace(); }
    }

    public final static void main(String[] args) {
        if (args.length == 0)
            return;

        printFile(args[0]);
    }
}

HexViewer - r2

And see how it now handles file endings when the file size is not a multiple of 16 :p

Picture 2

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

How to make your own Quick & Dirty Hex File Viewer in Java

Saturday, April 18th, 2009

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 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 “.”

Enjoy:

//HexViewer.java
import java.io.*;

public final class HexViewer {
    public final static void printFile(String filePath) {
        File f;
        try {
            f = new File(filePath);
        } catch (Exception e) {
            return;
        }

        try {

            FileInputStream fis = new FileInputStream(f);
            while (fis.available() > 0) {
                char[] line = new char[16];
                for (int i=0; i < 16; i++) {
                    int readByte = fis.read();
                    String paddingZero = (readByte < 16) ? "0" : "";
                    System.out.print(paddingZero + Integer.toHexString(readByte) + " ");
                    line[i] = (readByte >= 33 && readByte <= 126) ? (char) readByte : '.';
                }
                System.out.println(new String(line));
            }
        } catch (Exception e1) { e1.printStackTrace(); }
    }

    public final static void main(String[] args) {
        if (args.length == 0)
            return;

        printFile(args[0]);
    }
}

Usage:

java HexViewer &lt;path to file> | less
java HexViewer Desktop/Puppet.wmv | less

30 26 b2 75 8e 66 cf 11 a6 d9 00 aa 00 62 ce 6c 0&.u.f.......b.l
74 14 00 00 00 00 00 00 07 00 00 00 01 02 a1 dc t...............
ab 8c 47 a9 cf 11 8e e4 00 c0 0c 20 53 65 68 00 ..G.........Seh.
00 00 00 00 00 00 f4 10 68 f9 49 76 2e 43 b5 9f ........h.Iv.C..
09 0a 2d 19 45 7c 32 3a 24 00 00 00 00 00 70 ee ..-.E|2:$.....p.
14 06 fb da c7 01 28 01 00 00 00 00 00 00 00 8c ......(.........
bb 53 00 00 00 00 10 88 dd 52 00 00 00 00 b8 0b .S.......R......
00 00 00 00 00 00 02 00 00 00 40 1f 00 00 40 1f ..........@...@.
00 00 90 50 02 00 b5 03 bf 5f 2e a9 cf 11 8e e3 ...P....._......
00 c0 0c 20 53 65 61 10 00 00 00 00 00 00 11 d2 ....Sea.........
d3 ab ba a9 cf 11 8e e6 00 c0 0c 20 53 65 06 00 ............Se..
33 10 00 00 a9 46 43 7c e0 ef fc 4b b2 29 39 3e 3....FC|...K.)9>
de 41 5c 85 27 00 00 00 00 00 00 00 01 00 0c 65 .A\.'..........e
00 6e 00 2d 00 61 00 75 00 00 00 5d 8b f1 26 84 .n.-.a.u...]..&.
45 ec 47 9f 5f 0e 65 1f 04 52 c9 1a 00 00 00 00 E.G._.e..R......
00 00 00 02 01 ea cb f8 c5 af 5b 77 48 84 67 aa ..........[wH.g.
8c 44 fa 4c ca 62 01 00 00 00 00 00 00 06 00 00 .D.L.b..........
00 01 00 0c 00 02 00 02 00 00 00 49 00 73 00 56 ...........I.s.V
00 42 00 52 00 00 00 00 00 00 00 01 00 34 00 00 .B.R.........4..
00 06 00 00 00 44 00 65 00 76 00 69 00 63 00 65 .....D.e.v.i.c.e
00 43 00 6f 00 6e 00 66 00 6f 00 72 00 6d 00 61 .C.o.n.f.o.r.m.a
00 6e 00 63 00 65 00 54 00 65 00 6d 00 70 00 6c .n.c.e.T.e.m.p.l
00 61 00 74 00 65 00 00 00 4c 00 31 00 00 00 00 .a.t.e...L.1....
00 02 00 0c 00 02 00 02 00 00 00 49 00 73 00 56 ...........I.s.V
00 42 00 52 00 00 00 01 00 00 00 02 00 34 00 00 .B.R.........4..
00 0c 00 00 00 44 00 65 00 76 00 69 00 63 00 65 .....D.e.v.i.c.e
00 43 00 6f 00 6e 00 66 00 6f 00 72 00 6d 00 61 .C.o.n.f.o.r.m.a
00 6e 00 63 00 65 00 54 00 65 00 6d 00 70 00 6c .n.c.e.T.e.m.p.l
00 61 00 74 00 65 00 00 00 4d 00 50 00 40 00 4d .a.t.e...M.P.@.M
00 4c 00 00 00 00 00 01 00 2e 00 03 00 04 00 00 .L..............
00 57 00 4d 00 2f 00 57 00 4d 00 41 00 44 00 52 .W.M./.W.M.A.D.R
00 43 00 50 00 65 00 61 00 6b 00 52 00 65 00 66 .C.P.e.a.k.R.e.f
00 65 00 72 00 65 00 6e 00 63 00 65 00 00 00 a7 .e.r.e.n.c.e....
3f 00 00 00 00 01 00 34 00 03 00 04 00 00 00 57 ?......4.......W
00 4d 00 2f 00 57 00 4d 00 41 00 44 00 52 00 43 .M./.W.M.A.D.R.C
00 41 00 76 00 65 00 72 00 61 00 67 00 65 00 52 .A.v.e.r.a.g.e.R
00 65 00 66 00 65 00 72 00 65 00 6e 00 63 00 65 .e.f.e.r.e.n.c.e
00 00 00 b0 06 00 00 74 d4 06 18 df ca 09 45 a4 .......t......E.
ba 9a ab cb 96 aa e8 a4 0d 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................

This how it looks on a full blown hex editor like HexEdit:
Same file on HexEdit

The Code of HexViewer.java on emacs:
HexViewer.java on emacs

Screenshot of the output:
HexViewer in action

Homework
Hack the code so that it ouputs the first column shown on the HexEdit screenshot. That column represents the byte position of each row. It’s basically a counter incremented 16 units at the time, and shown in Hex.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

OnLive could change the video game industry

Wednesday, March 25th, 2009

I feel it’s my geek given duty to make a post about this presentation. I was lucky to finally have the time to watch their hour long presentation and Q&A session at the Game Developer Conference 2009 (which ends a couple of days from today). They could have not picked a better place to finally demo their technology.

In short, they’ve introduced a huge new concept to the video game industry, I’d call it “Cloud Gaming” to not only host the games, but also host the processing juice. You won’t need a console anymore, they keep the hardware to execute and stream the game to your screen. They support TV (with a miniconsole), PC and Mac.

So bear with me, they say they have solved the issue that you’re thinking about now, Lag. The people behind this worked on apple to create Quicktime, and they identified differences between what it takes to compress linear (regular) video, vs Interactive Video. They say their compression algorithm doesn’t take seconds of lag (like when you stream over a webcam), but miliseconds. They have custom chips to process the graphics, and I bet they might even built their own network protocol right on top of IP.

So what are some of the implications of this:

  • We’ll all be able to finally play Crysis and even more demanding games on low end PCs
  • No more buying more hardware, no more upgrading your PC to be able to run games, no more buying consoles
  • All your games live on the platform, so you can play from any computer, and you’ll keep the state of your game until the last time you hit the Pause button
  • Your friends can see you play, live. I bet we’ll be able to see live tournaments, we’ll start seeing a new breed of famous people get more attention, the Elite gamers. Imagine seeing the best Call of Duty player in the world playing live
  • New Genres of video games will emerge on this platform, maybe even new genres of entertainment, think new Live Broadcast shows where participants use an avatar to either act or compete (game show)
  • Game Developers not need to think of the rendering limitations that they might have nowadays, and will be able to design games that could only be imagined in the past. Render quality only thought for movies will now exist for video games, think of virtual reality now
  • There’s about 100 million PCs/Macs/Laptops out there that are not ready today to play high end games, now they’ll have the possibility of playing virtually any game by installing a 1Mb plugin from OnLive.com
  • Takes Piracy out of the Business Equation
  • A bigger gaming audience makes an even better case for companies placing advertisement in video games, maybe there will be a lot more high end free games with bigger audiences, think the next Grand Theft Auto coming out for free with superb real life like graphics rendering, all ad sponsored and free to the consumer. The amount of people that you could have playing a great game for free would make other developers think twice about charging for their games and having their virtual worlds ad sponsored.
  • No more installs
  • Now multiplayer will have almost no latency since all players live inside their datacenter, you only get the latency of your ISP if there’s any
  • Nintendo, Sony and Microsoft must be shitting their pants
  • Services that sell used games are going to be selling vintage and their business will be reduced

However I think there will always be room for the old consoles. This is the biggest entertainment industry in the world, we have grown up with consoles for almost 30 years and there’s a lot of changes to push into people’s minds:

  • How do you convince me, that finally made up my mind after years and dropped $500 on a PS3 to play with my friends in latin america online to switch to this, if my friends will probably have no way to even have access to the system in years to come?
  • How do you convince PC gamers that rather pay for the hardware and pirate the games? (There’s plenty of those, probably the majority of the PC gaming population outside the US never plays for a PC game, and doesn’t get into consoles because they pirate the games) into renting or buying games on the cloud?
  • How do you convince all the people that they should switch when their gaming experience depends entirely on being connected to the internet. So If the ISP is having issues I can’t play? isn’t my console awesome?

It seems that many of these complains are similar to all the complains brought upon business models that didn’t exist online and that are now thriving. This presentation left me with my mouth wide open, and I highly recommend you watch it. You’ll be blown away by the power of the UI, and how as you Browse for games, you can even see how other people are playing live, it’s like streaming video is nothing for OnLive. Really sick technology.

In the case that they succeed, I just can’t wait for them to have competition by the existing big brands, it’s going to get so interesting once cloud gaming becomes the defacto platform, maybe we as consumers will end up playing games for free, all sponsored with in game ads.

Just by listening to the guy if you’re a techie, your mind will start to fly to barely start to imagine the awesomeness of the technology that should be behind this. I can imagine anything from custom virtualization technology, to custom GPUs, custom network cards, custom network protocols on top of IP, deals with major internet backbone networks, ISPs, deals with game publishers, sick level API development, incomprehensible comprehension technology for my retard brain… when I see shit like this, I always think… how the hell is there people that still believe in god? Mankind is the closest thing there is to something like that! I’m thankful for people in this world that can think so big.

The service is supposed to launch next Winter 2009, but you can sign up to be a beta tester

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Delete All Direct Messages of your Twitter Account at once (or at least try!)

Friday, February 13th, 2009

Since Twitter doesn’t provide with a “Delete All Direct Messages” functionality, Here’s a Python script that attempts to delete all the direct messages stored on your Twitter account.

Limitations
The only problem with it is that given the limitations of the Twitter REST API, I was forced to send a request per message to be deleted, and it seems that Twitter will only allow 100 requests per hour (per client).

So in theory you will be able to delete 100 an hour, although I have seen it delete over 400 messages Twitter gets all grumpy on me.

Usage
Just save the script as twitter_delete_direct_messages.py, open a terminal and run it:

python twitter_delete_direct_messages.py

After that just follow the instructions on screen and enjoy as messages get wiped out.

If you find this useful, you can thank me by following me on Twitter.

Get The Script

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Using a linear array as a bidimensional matrix

Tuesday, January 27th, 2009

Often times I find the need to use a list or linear array as if it was a table.

Everytime I need to do so, I always end up coding functions to convert a (x,y) coordinate to the real index n in the array.

Let me illustrate, with an example. You have a string that defines the elements of a game board, and you want to work using (x,y) coordinates.

s="xxxxx@xx@xxx@@xx"

If you were to look at it as a matrix (width=4), it’d be something like this

s="xxxx
   x@xx
   @xxx
   @@xx"

However, I can’t do
s[x,y], since it’s a linear array, it’s a string.

You need to convert from (x,y) to a number that represents an index in the array.

This is very simple:

width=4
def getNforXY(x,y):
  return x + width*y

What if you want to do it backwards. What if you need to know what’s the X and Y for a given index N in the string?

def getXYforN(n):
  y = int(n/width)
  x = n - width/y
  return (x,y)

Cheers

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

jQuery tag wrapping

Friday, December 19th, 2008

Many times you’ll be working on something in HTML that could be long and repetitive, and then for some reason you need to edit the entire thing to wrap each of the tags on some other tags, a common example would be to link many elements.

Take this example, there’s a bunch of photos, they’re all contained in divs that have a css class called “photos”, inside we just throw
a bunch of <img> tags, and all of a sudden our boss tells us he wans to have every image on that page linked to “http://domain.com/photos”. jQuery to the rescue, no need to wrap each <img> with <a href=”"> by hand, you can do it all in one line

So if the code looks like this:

       <div class="photos">
            <img src="http://domain.com/0670027481_m.jpg"/>
            <img src="http://domain.com/211161aa99_m.jpg"/>
            <img src="http://domain.com/173bb0cd6_m.jpg"/>
        </div>
        <div class="photos">
            <img src="http://domain.com/841fd90b5b_m.jpg"/>
            <img src="http://domain.com/52dda2cee5_m.jpg"/>
            <img src="http://domain.com/b569399599_m.jpg"/>
        </div>
        <div class="photos">
            <img src="http://domain.com/8806e863a4_m.jpg"/>
            <img src="http://domain.com/5e43aa95fe_m.jpg"/>
            <img src="http://domain.com/68a74a088c_m.jpg"/>
        </div>

        <div class="tshirt-photos">
            <img src="http://farm4.static.flickr.com/3149/3113619519_72ce82c545_m.jpg"/>
            <img src="http://farm4.static.flickr.com/3236/3105792155_99f2388869_m.jpg"/>
            <img src="http://farm3.static.flickr.com/2114/2372582266_765842fae9_m.jpg"/>
        </div>

With jQuery you can match all the <img> elements, and wrap them all with “<a href=”http://domain.com/photos”></a>” in a single line of code:

$(document).ready(function() { $(".photos img").wrap('<a href="http://domain.com/photos">') })

So to explain how it works, when the document is ready (when it’s finished loading) the function inside is called back, it will use the jQuery selector $(“.photos img”) to match all the <img> tags contained withing elements of css class “photos”, then it applies the wrap() function, which will wrap the matched elements with the given html tags.

About jQuery

jQuery is a lightweight JavaScript library that emphasizes interaction between JavaScript and HTML. It was released January 2006 at BarCamp NYC by John Resig.

Dual licensed under the MIT License and the GNU General Public License, jQuery is free and open source software.

jQuery contains the following features:

  • DOM element selections
  • DOM traversal and modification, (including support for CSS 1-3 and basic XPath)
  • Events
  • CSS manipulation
  • Effects and animations
  • Ajax
  • Extensibility
  • Utilities – such as browser version and the each function.
  • JavaScript Plugins

jQuery Documentation

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)