function convertHEX(num) {
var hex = '';
for (i=0;i<num.length;i++) {
if (num.charCodeAt(i).toString(16).toUpperCase().length < 2) {
hex += "0" + num.charCodeAt(i).toString(16).toUpperCase();
} else {
hex += "x20" + num.charCodeAt(i).toString(16).toUpperCase();
}
}
return hex;
}
var text = 'любой текст';
document.write(convertHEX(text));