JavaScript convert_uuencode
uuencode a string
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 67 68 69 7071 72 73 74 7576 77 78 | function convert_uuencode (str){ // uuencode a string // // version: 1008.1718 // discuss at: http://phpjs.org/functions/convert_uuencode // + original by: Ole Vrijenhoek // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + reimplemented by: Ole Vrijenhoek // + bugfixed by: Brett Zamir (http://brett-zamir.me) // - depends on: is_scalar // * example 1: convert_uuencode("test\ntext text\r\n"); // * returns 1: '0=&5S=`IT97AT('1E>'0-"@``' // shortcut var chr = function (c) { return String.fromCharCode(c); }; if (!str || str=="") { return chr(0); } else if (!this.is_scalar(str)) { return false; } var c = 0, u = 0, i = 0, a = 0; var encoded = "", tmp1 = "", tmp2 = "", bytes = {}; // divide string into chunks of 45 characters var chunk = function () { bytes = str.substr(u, 45); for (i in bytes) { bytes[i] = bytes[i].charCodeAt(0); } if (bytes.length != 0) { return bytes.length; } else { return 0; } }; while (chunk() !== 0) { c = chunk(); u += 45; // New line encoded data starts with number of bytes encoded. encoded += chr(c+32); // Convert each char in bytes[] to a byte for (i in bytes) { tmp1 = bytes[i].charCodeAt(0).toString(2); while (tmp1.length < 8) { tmp1 = "0" + tmp1; } tmp2 += tmp1; } while (tmp2.length % 6) { tmp2 = tmp2 + "0"; } for (i=0; i<=(tmp2.length/6)-1; i++) { tmp1 = tmp2.substr(a, 6); if (tmp1 == "000000") { encoded += chr(96); } else { encoded += chr(parseInt(tmp1, 2)+32); } a += 6; } a = 0; tmp2 = ""; encoded += "\n"; } // Add termination characters encoded += chr(96)+"\n"; return encoded; } |
Examples
Running
1 | convert_uuencode("test\ntext text\r\n"); |
Should return
1 | '0=&5S=`IT97AT('1E>'0-"@``' |
Dependencies
In order to use this function, you also need:
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have convert_uuencode goodness in JavaScript.
I did some reaearch for this function,
in php it uses linebreaks and stuff...
I fixed it with this function
// author: Ole Vrijenhoek
// sources: http://www.herongyang.com/encoding/UUEncode-PHP-Implementation.html
// http://en.wikipedia.org/wiki/Uuencode
// depends on is_scalar()
function convert_uuencode(str){
// shortcut
var char = function(c) {
return String.fromCharCode(c);
};
if(!str || str=="") {
return char(0);
} else if(!is_scalar(str)) {
return false;
}
var c = 0, u = 0, i = 0, a = 0
var encoded = "", tmp1 = "", tmp2 = "", bytes = {}, b = {};
var b0 = 0, b1 = 0, b2 = 0, b3 = 0;
// divide string into chunks of 45 characters
var chunk = function() {
bytes = str.substr(u, 45);
for(i in bytes) {
bytes[i] = bytes[i].charCodeAt(0);
}
if(bytes.length != 0) {
return bytes.length;
} else {
return 0;
}
};
while(chunk() !== 0) {
c = chunk();
u += 45;
while(c % 3) {
bytes[c++] = char(0);
}
// New line encoded data starts with number of bytes encoded.
encoded += char(c+32);
// Convert each char in bytes[] to a byte
for(i in bytes) {
tmp1 = bytes[i].charCodeAt(0).toString(2);
while(tmp1.length < 8) {
tmp1 = "0" + tmp1;
}
tmp2 += tmp1;
}
for(i=0; i<=(tmp2.length/6)-1; i++) {
tmp1 = tmp2.substr(a, 6);
if(tmp1 == "000000") {
encoded += char(96);
} else {
encoded += char(parseInt(tmp1, "2")+32);
}
a += 6;
}
encoded += "\n";
}
// Add termination characters
encoded += char(96)+"\n";
return encoded;
}


Ole Vrijenhoek
26 Apr '09
function convert_uuencode(str){ // shortcut var char = function(c) { return String.fromCharCode(c); }; if(!str || str=="") { return char(0); } else if(!is_scalar(str)) { return false; } var c = 0, u = 0, i = 0, a = 0 var encoded = "", tmp1 = "", tmp2 = "", bytes = {}; // divide string into chunks of 45 characters var chunk = function() { bytes = str.substr(u, 45); for(i in bytes) { bytes[i] = bytes[i].charCodeAt(0); } if(bytes.length != 0) { return bytes.length; } else { return 0; } }; while(chunk() !== 0) { c = chunk(); u += 45; // New line encoded data starts with number of bytes encoded. encoded += char(c+32); // Convert each char in bytes[] to a byte for(i in bytes) { tmp1 = bytes[i].charCodeAt(0).toString(2); while(tmp1.length < 8) { tmp1 = "0" + tmp1; } tmp2 += tmp1; } while(tmp2.length % 6) { tmp2 = tmp2 + "0"; } for(i=0; i<=(tmp2.length/6)-1; i++) { tmp1 = tmp2.substr(a, 6); if(tmp1 == "000000") { encoded += char(96); } else { encoded += char(parseInt(tmp1, "2")+32); } a += 6; } a = 0, tmp2 = ""; encoded += "\n"; } // Add termination characters encoded += char(96)+"\n"; return encoded; }