You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
850 B
33 lines
850 B
var charenc = {
|
|
// UTF-8 encoding
|
|
utf8: {
|
|
// Convert a string to a byte array
|
|
stringToBytes: function(str) {
|
|
return charenc.bin.stringToBytes(unescape(encodeURIComponent(str)));
|
|
},
|
|
|
|
// Convert a byte array to a string
|
|
bytesToString: function(bytes) {
|
|
return decodeURIComponent(escape(charenc.bin.bytesToString(bytes)));
|
|
}
|
|
},
|
|
|
|
// Binary encoding
|
|
bin: {
|
|
// Convert a string to a byte array
|
|
stringToBytes: function(str) {
|
|
for (var bytes = [], i = 0; i < str.length; i++)
|
|
bytes.push(str.charCodeAt(i) & 0xFF);
|
|
return bytes;
|
|
},
|
|
|
|
// Convert a byte array to a string
|
|
bytesToString: function(bytes) {
|
|
for (var str = [], i = 0; i < bytes.length; i++)
|
|
str.push(String.fromCharCode(bytes[i]));
|
|
return str.join('');
|
|
}
|
|
}
|
|
};
|
|
|
|
module.exports = charenc;
|
|
|