This is an issue I have been dealing with for months, and I would really appreciate any help.
I am encoding a string and writing the output to MyFile.js using the function below. While debugging my example, char encodedCode[] displayed as integers has length 12 and these values: 97,0,108,101,114,116,40,34,80,114,0,105.
MyFile.js is correctly generated with the expected size and characters, as I can see from Notepad++.
MyFileServlet.java
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setCharacterEncoding("iso-8859-1");
res.setHeader("Content-Type", "text/javascript;charset=iso-8859-1");
res.setHeader("Content-Disposition","attachment;filename=MyFile.js");
PrintWriter printer=res.getWriter();
char encodedCode[]=encode(req.getParameter("originalScript"));
printer.write(encodedCode);
printer.close();
}
However, when I try to run that MyFile.js with some debugging code (see below) from MyFile.html, I get different behaviour depending on the browser:
- Chrome/Firefox:
length 12values97,0,108,101,114,116,40,34,80,114,0,105 - Internet Explorer 9:
length 10values97,108,101,114,116,40,34,80,114,105
So IE is ignoring all the “0” characters. Why? Thank you very much
JSFIDDLE: http://jsfiddle.net/vnqL2/
Edit: I tried writing those bytes to an external binary file MyFile.xxx and reading them from Javascript/JQuery/Ajax/XMLHttpRequest and I still get the same issue with IE.
MyFile.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>My Page</title>
</head>
<body>
<script type="text/javascript" charset="iso-8859-1" src="MyFile.js">
</script>
</body>
</html>
MyFile.js
function d(a)
{
a=(a+"").split("");
var l=a.length,x=[l];
alert(l);
for(i=0;i<l;i++){
x[i]=a[i].charCodeAt(0);
document.write(x[i]);
}
};
d("HERE GO THOSE BYTES HARDCODED");
I do not know what is your purpose but NULL bytes inside strings are not legit.
On the other hand you could easily reconstruct an array of bytes (integers) by transmitting the bytes as a comma seperated list of integers. Or another approach would be a json file.
Example code:
Test it here