Created
September 17, 2011 13:48
-
-
Save martok/1223953 to your computer and use it in GitHub Desktop.
ASCIIoverUTF8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Ref: http://twitter.com/BenBE1987/statuses/115050893115797505 | |
ASCIIoverUTF8 = { | |
write: function(msg) { | |
var result = ''; | |
(msg.length%2) && (msg+=String.fromCharCode(0)); | |
for (var i=0;i<msg.length; i+=2) { | |
var a=msg.charCodeAt(i)&0xFF, | |
b=msg.charCodeAt(i+1)&0xFF; | |
result+=String.fromCharCode((a<<8) | b); | |
} | |
return result; | |
}, | |
read: function(utf) { | |
var result = ''; | |
for (var i=0;i<utf.length; i++) { | |
var m=utf.charCodeAt(i); | |
result+=String.fromCharCode((m>>8) & 0xFF) + String.fromCharCode(m & 0xFF); | |
} | |
return result; | |
} | |
}; | |
//Usage: | |
message='Hallo Welt!'; | |
var x = ASCIIoverUTF8.write(message); | |
console.log('#AoU ',x); | |
var y = ASCIIoverUTF8.read(x); | |
console.log('Normal: ',y); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
haha :D