JavaScript - how to view raw data for an image

Is it possible to view the raw data for an image file in javascript?

I'm trying to write a script that converts an image into its hex dump.

How can I view the data I'm writing to the image file?

Answers:

Answer

You can do this with XHR:

var xhr = new XMLHttpRequest();
xhr.open('GET', '/my/image/file.png', true);
xhr.responseType = 'arraybuffer'; // this will accept the response as an ArrayBuffer
xhr.onload = function(buffer) {
    var words = new Uint32Array(buffer),
        hex = '';
    for (var i = 0; i < words.length; i++) {
      hex += words.get(i).toString(16);  // this will convert it to a 4byte hex string
    }
    console.log(hex);
};
xhr.send();

Look at the doc for ArrayBuffers and TypedArrays

And you can see how I use it in testing here

Answer

Is it possible to get the source code of a file in javascript?

Find the URL for the script and load that into your browser. (Or use Firebug)

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.