hexdump in the browser

2009-09-23

This morning, I thought: why is XMLHttpReq for xml and text but not for binary files? It turns out you can use it for binary files, but not in each browser and only for remote files. I've written a small implementation of hexdump. It loads a binary file like this:

function load_binary_resource(url) {  
  // this works in firefox, chromium and arora,
  // but binary files are read only partially in konqueror and opera
  var req = new XMLHttpRequest();  
  req.open('GET', url, false);  
  req.overrideMimeType('text/plain; charset=x-user-defined');  
  req.send(null);
  return req.responseText;  
}

Then you can access each byte with code like this:

var data = load_binary_resource(file);
var byte5 = data.charCodeAt(5) & 0xff;

As described here.
With these techniques I made hexdump in the browser.

Comments

Post a comment