(Created page with "var EightBit = {}; (function(){ // source: http://ecmanaut.blogspot.nl/2006/07/encoding-decoding-utf8-in-javascript.html var encodeUtf8 = function(s){ return unescape(enc...") |
|||
Line 16: | Line 16: | ||
} | } | ||
− | return | + | return DNA.encodeUInt(code, 4); |
}; | }; | ||
Latest revision as of 13:19, 8 July 2016
var EightBit = {};
(function(){ // source: http://ecmanaut.blogspot.nl/2006/07/encoding-decoding-utf8-in-javascript.html var encodeUtf8 = function(s){ return unescape(encodeURIComponent(s)); }; var decodeUtf8 = function(s){ return decodeURIComponent(escape(s)); };
var encChr = function(chr){ var code = chr.charCodeAt(0); if(code < 0 || code > 255){ // beyond 8 bit return null; }
return DNA.encodeUInt(code, 4); };
var encodeChar = function(str){ return encodeStr(str); };
var decodeChar = function(dna){ return String.fromCharCode(DNA.decodeUInt(dna)); };
var encodeStr = function(str){ str = encodeUtf8(str); var ret = ;
for(var i = 0; i < str.length; i++){ var chr = encChr(str[i]); if(chr === null){ return null; } ret += chr; }
return ret; };
var decodeStr = function(dna){ var ret = ;
for(var i = 0; i < dna.length; i += 4){ var chr = decodeChar(dna.substr(i, 4)); if(chr === null){ return null; } ret += chr; }
return decodeUtf8(ret); };
EightBit = { 'encodeChar': encodeChar, 'decodeChar': decodeChar, 'encodeStr': encodeStr, 'decodeStr': decodeStr }; })();