| 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; |