Skip to content

Instantly share code, notes, and snippets.

@martok
Created September 17, 2011 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martok/1223953 to your computer and use it in GitHub Desktop.
Save martok/1223953 to your computer and use it in GitHub Desktop.
ASCIIoverUTF8
//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);
@JesseKlugmann
Copy link

haha :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment