Reputation: 8602
I have a php script that generates a binary file (is some kind of compression of a map, doesn't really matter). I can save it as an image for example and send it.
What I need now is to make a string from that file. To be more clear... I want to get the bits (ignore the image overhead of course) and the final string must be something like 100101010101111... u get the idea.
Can and if yes how do I do this in javascript?
Thank you for your help.
Upvotes: 1
Views: 1164
Reputation: 63882
You can make an AJAX call to retrieve the contents of the file and then use bitwise operators to generate the binary representation string that you are striving for.
The php script must be accessible through a HTTP request, of course.
One of the easiest ways to implement a AJAX GET request is to use jQuery.get
, which you can read more about here.
Regarding the generation of a binary representation there are several implementations available online.
I wrote my own sample implementation of a string to binary converter:
var data = "h€llo world ";
var binary_string = "";
for (var idx in data) {
var v = data.charCodeAt (idx);
do {
var b = v & 0xFF;
for (var i =0; i < 8; ++i)
binary_string += (b & (1<<(7-i))) ? '1' : '0';
binary_string += ' ';
} while (v >>= 8);
}
binary_string = 01101000 10101100 00100000 01101100 01101100 01101111 00100000 01110111 01101111 01110010 01101100 01100100 00100000
Upvotes: 4