Reputation:
For XSS safety, I'd like to convert all characters to \u+hex encoding. I've seen UTF-16 solutions, but I can't find anything for UTF-8.
I first want to ensure that all characters are UTF-8. Here, I've seen many solutions but nothing consistent for javascript.
How can a string be tested for UTF-8 and return the \u+hex encoding if so yet false if not?
Upvotes: 0
Views: 1225
Reputation: 141
function string_to_hex(input) {
var temp = 0, output = "";
for (var i = 0; i < input.length; i++) {
temp = input.charCodeAt(i);
if (temp > 0xFF) {
// if (temp == 192 || temp == 193 || temp > 244) {
return false;
}
output += "\\x" + temp.toString(16).slice(1); //lazy padding
}
return output;
}
The line commented out isn't needed if you do not check for illegal code points.
Upvotes: 2