Convert data between UTF16 String and Words by using CryptoJS components.
Example:
<!DOCTYPE html>
<html>
<head>
<script src="core.js"></script>
<script src="enc-utf16.js"></script>
<script>
function testTo() {
var data = document.getElementById("inputData").value;
document.getElementById("result").innerHTML=CryptoJS.enc.Utf16LE.parse(data);
}
function testFrom() {
var data = document.getElementById("inputData").value;
var a = [];
for(var i=0; i<data.length; i+=8){
a.push('0x'+data.substring(i,i+8));
}
var words = CryptoJS.lib.WordArray.create(a);
document.getElementById("result").innerHTML=CryptoJS.enc.Utf16LE.stringify(words);
}
</script>
</head>
<body>
<input type="text" id="inputData" />
<button type="button" onclick="testTo()">to UTF16-LE</button><button type="button" onclick="testFrom()">from UTF16-LE</button>
<div id="result"></div>
</body>
</html>
Get core.js, enc-utf16.js from CryptoJS.
Download the example code
沒有留言:
張貼留言