How To Convert Hexadecimal Byte Stream In The Form Of String To Actual Bytestream In Javascript?
I have a pdf file that is available in the form of a Hexa string bytestream like: '255044462D312E330D0A25E2E3CFD30D0A322030206F626A......' (its a very long string, so I have just
Solution 1:
// To IE Boat method is here,
if (!window.btoa) {
var tableStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var table = tableStr.split("");
window.btoa = function (bin) {
for (var i = 0, j = 0, len = bin.length / 3, base64 = []; i < len; ++i) {
var a = bin.charCodeAt(j++), b = bin.charCodeAt(j++), c = bin.charCodeAt(j++);
if ((a | b | c) > 255) throw new Error("String contains an invalid character");
base64[base64.length] = table[a >> 2] + table[((a << 4) & 63) | (b >> 4)] +
(isNaN(b) ? "=" : table[((b << 2) & 63) | (c >> 6)]) +
(isNaN(b + c) ? "=" : table[c & 63]);
}
return base64.join("");
};
}
//script block
// try this way to open you pdf file like this in javascript hope it will help you.
function hexToBase64(str)
{
return btoa(String.fromCharCode.apply(null,str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
}
var data= hexToBase64("255044462D312E330D0A25E2E3CFD30D0A322030206F626A");// here pass the big hex string
// it will be open in the web browser like this
document.location.href = 'data:application/pdf;base64,' +data;
**//to open up in the new window**
window.open('data:application/pdf;base64,' +data, "_blank", "directories=no, status=no, menubar=no, scrollbars=yes, resizable=no,width=600, height=280,top=200,left=200");
Post a Comment for "How To Convert Hexadecimal Byte Stream In The Form Of String To Actual Bytestream In Javascript?"