如何将字符串形式的十六进制字节流转换为javascript中的实际字节流? [英] How to convert Hexadecimal Byte stream in the form of string to actual bytestream in javascript?
问题描述
我有一个 pdf 文件,它以十六进制字符串字节流的形式提供,例如:
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 given the format) which is in a string format available in the browser.
我需要将其转换为十六进制格式,以便我可以通过将此输出流写入 pdf 文件来在 Web 浏览器中呈现 pdf.我需要知道是否有任何内置功能或可以在 Javascript 中实现的任何方式.
I need to convert this into its hexadecimal format so that I can render the pdf in the web browser by writing this output stream to the pdf file. I need to know if there is any inbuilt function or any way this can be achieved in Javascript.
我知道用 Java 实现这个功能很简单,但我有一个 ABAP
后端,它只获取我这个字符串和 SAPUI5
前端,它是一个基于 Javascript 的框架.
I know implementing this functionality is simple with Java but I have an ABAP
backend that only fetches me this string and SAPUI5
front end that is a framework based on Javascript.
我通过编写一个简单的 Java 程序来检查这个字节流的有效性,该程序生成 PDF 只是为了测试数据是否正确:
I checked the validity this bytestream by writing a simple java program that generated the PDF just for testing purpose if the data is coming right:
public static void main(String[] args) {
FileOutputStream fop = null;
File file;
file = new File("C:/Users/I074098/Desktop/Project Temps/test.pdf");
fop = new FileOutputStream(file);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
//I manually added "(byte) 0x" in the above string and
//converted it to the following format, I skipped that conversion in this code
byte[] contentInBytes = new byte[]{
(byte) 0x25,(byte) 0x50,(byte) 0x44,(byte) 0x46, .....
}
fop.write(contentInBytes);
fop.flush();
fop.close();
}
这会生成pdf.但我知道它非常低效,我不确定所有这些都可以在 javascript 中完成.我做了很多搜索,但没有结果.我会感谢任何帮助.
This generates the pdf. But I know it is very inefficient and I am not sure all this can be done in javascript. I have done a lot of search and it was not fruitful. I will be thankful any and every help.
问候,里斯旺
推荐答案
// 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");
这篇关于如何将字符串形式的十六进制字节流转换为javascript中的实际字节流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!