Skip to content

Commit 7b6c379

Browse files
committed
arbitrary radix support
1 parent c05a5b2 commit 7b6c379

File tree

9 files changed

+60
-12
lines changed

9 files changed

+60
-12
lines changed

dist/full/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<li><input type="radio" name="mode" value="9" id="rad9"><label for="rad9">Base64</label></li>
4646
<li><input type="radio" name="mode" value="10" id="rad10"><label for="rad10">Ascii85</label></li>
4747
<li><input type="radio" name="mode" value="1" id="rad1"><label for="rad1">Morse</label></li>
48+
<li><input type="radio" name="mode" value="14" id="rad14"><label for="rad14">Radix-N</label></li>
4849
</ul>
4950
<div id="border"></div>
5051
<div class="toolopen"><img src="img/tools.png" onclick="tbox_toggle()" alt="tools" title="text tools"></div>

dist/full/js/ui.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
if(typeof addcredits == 'function') addcredits("ui.js",2.0,"crashdemons","JXlate UI script and overarching operations.")
33
var oForm=null;
44
var mode=0;
5-
var mode_bases=[256,'mc',2,8,10,16,'32r','32h','32c',64,85,'ue','ucs2','utf8'];//values used internally to represent each base in shorthand.
5+
var mode_bases=[256,'mc',2,8,10,16,'32r','32h','32c',64,85,'ue','ucs2','utf8','n'];//values used internally to represent each base in shorthand.
66
//var mode_names=['Text','Morse','Binary','Octal','Decimal','Hexadecimal','Base32Rfc','Base32Hex','Base32Ckr','Base64','Ascii85','UrlEncode'];//unused array
77

88
function xlate_bytesNF(s,baseFrom){//translate a string from one base to another
@@ -44,7 +44,9 @@ function xlate_poll(){//poll the UI for mode radio-box changes.
4444
xlate_switch(oldmode,newmode);//trigger a translation
4545
}catch(e){
4646
setMode(oldmode);
47-
alert("This value could not be converted as specified.\nPlease make sure it is valid.")
47+
alert("This value could not be converted as specified.\nPlease make sure it is valid.\n\n"
48+
+"Technical Reason: \n"+" "+e
49+
)
4850
}
4951
console.log("conversion complete");
5052
}

dist/full/js/xlate.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fill_bases();
88
function fill_bases(){ for(var b=32;b>=0;b--) base_charsets[b]=b32hex.substr(0,b).split(""); }//set some charset options for use in conversion functions.
99

1010
//check if the base ID is a numeral conversion or an encoding (external function)
11-
function isEncodedBase(base){ return (base==="32r" ||base==="32h" ||base==="32c" || base===64 || base===85 || base==="mc" || base==="ue" || base==="ucs2" || base==="utf8");}
11+
function isEncodedBase(base){ return (base==="32r" ||base==="32h" ||base==="32c" || base===64 || base===85 || base==="mc" || base==="ue" || base==="ucs2" || base==="utf8" || base==="n");}
1212

1313

1414
//resolves any encodings before regular numeral conversions.
@@ -28,6 +28,7 @@ function array_prepareEncodings(a,baseFrom,baseTo){
2828
else if(baseFrom==="mc" ) s=morse_decode(a);
2929
else if(baseFrom==="ucs2") s=convert_encoding(a[0],'ucs2','iso88591');
3030
else if(baseFrom==="utf8") s=convert_encoding(a[0],'utf8','iso88591');
31+
else if(baseFrom==="n") s=array_base2base(a,radix_prompt(),256).join("");
3132
a=s.split("");//decode the single BaseX entry into chars (base256)
3233
baseFrom=256;//set up the parameter for the char->numeral array conversion.
3334
}
@@ -43,11 +44,19 @@ function array_prepareEncodings(a,baseFrom,baseTo){
4344
else if(baseTo==="mc" ) a=[ morse_encode(s)];
4445
else if(baseTo==="ucs2") a=[convert_encoding(s,'iso88591','ucs2')];
4546
else if(baseTo==="utf8") a=[convert_encoding(s,'iso88591','utf8')];
47+
else if(baseTo==="n") a=array_base2base(a,256,radix_prompt());
4648
baseFrom=baseTo;//we've encoded this to the new base, so lets set From to the current state - which disables any base conversion in array_base2base
4749
}
4850

4951
return [a,baseFrom,baseTo];//output modified parameters.
5052
}
53+
function radix_prompt(){
54+
var n = prompt("Please enter the radix (base) to convert with. (only 2-32 supported)", "");
55+
if (n === null) throw "no entry";
56+
n=parseInt(n);
57+
if(n<2 || n>32) throw "invalid radix";
58+
return n;
59+
}
5160
function array_base2base(a,baseFrom,baseTo){//convert arrays of numerals from one base to another - implements support for Base64
5261
var params=array_prepareEncodings(a,baseFrom,baseTo);//resolves any encodings before regular numeral conversions.
5362
a=params[0];//our prep function returns the parameters as an array after it's done, let's get them back where they need to be.

dist/monolithic/index.html

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@
189189
if(typeof addcredits == 'function') addcredits("ui.js",2.0,"crashdemons","JXlate UI script and overarching operations.")
190190
var oForm=null;
191191
var mode=0;
192-
var mode_bases=[256,'mc',2,8,10,16,'32r','32h','32c',64,85,'ue','ucs2','utf8'];//values used internally to represent each base in shorthand.
192+
var mode_bases=[256,'mc',2,8,10,16,'32r','32h','32c',64,85,'ue','ucs2','utf8','n'];//values used internally to represent each base in shorthand.
193193
//var mode_names=['Text','Morse','Binary','Octal','Decimal','Hexadecimal','Base32Rfc','Base32Hex','Base32Ckr','Base64','Ascii85','UrlEncode'];//unused array
194194

195195
function xlate_bytesNF(s,baseFrom){//translate a string from one base to another
@@ -231,7 +231,9 @@
231231
xlate_switch(oldmode,newmode);//trigger a translation
232232
}catch(e){
233233
setMode(oldmode);
234-
alert("This value could not be converted as specified.\nPlease make sure it is valid.")
234+
alert("This value could not be converted as specified.\nPlease make sure it is valid.\n\n"
235+
+"Technical Reason: \n"+" "+e
236+
)
235237
}
236238
console.log("conversion complete");
237239
}
@@ -1637,7 +1639,7 @@
16371639
function fill_bases(){ for(var b=32;b>=0;b--) base_charsets[b]=b32hex.substr(0,b).split(""); }//set some charset options for use in conversion functions.
16381640

16391641
//check if the base ID is a numeral conversion or an encoding (external function)
1640-
function isEncodedBase(base){ return (base==="32r" ||base==="32h" ||base==="32c" || base===64 || base===85 || base==="mc" || base==="ue" || base==="ucs2" || base==="utf8");}
1642+
function isEncodedBase(base){ return (base==="32r" ||base==="32h" ||base==="32c" || base===64 || base===85 || base==="mc" || base==="ue" || base==="ucs2" || base==="utf8" || base==="n");}
16411643

16421644

16431645
//resolves any encodings before regular numeral conversions.
@@ -1657,6 +1659,7 @@
16571659
else if(baseFrom==="mc" ) s=morse_decode(a);
16581660
else if(baseFrom==="ucs2") s=convert_encoding(a[0],'ucs2','iso88591');
16591661
else if(baseFrom==="utf8") s=convert_encoding(a[0],'utf8','iso88591');
1662+
else if(baseFrom==="n") s=array_base2base(a,radix_prompt(),256).join("");
16601663
a=s.split("");//decode the single BaseX entry into chars (base256)
16611664
baseFrom=256;//set up the parameter for the char->numeral array conversion.
16621665
}
@@ -1672,11 +1675,19 @@
16721675
else if(baseTo==="mc" ) a=[ morse_encode(s)];
16731676
else if(baseTo==="ucs2") a=[convert_encoding(s,'iso88591','ucs2')];
16741677
else if(baseTo==="utf8") a=[convert_encoding(s,'iso88591','utf8')];
1678+
else if(baseTo==="n") a=array_base2base(a,256,radix_prompt());
16751679
baseFrom=baseTo;//we've encoded this to the new base, so lets set From to the current state - which disables any base conversion in array_base2base
16761680
}
16771681

16781682
return [a,baseFrom,baseTo];//output modified parameters.
16791683
}
1684+
function radix_prompt(){
1685+
var n = prompt("Please enter the radix (base) to convert with. (only 2-32 supported)", "");
1686+
if (n === null) throw "no entry";
1687+
n=parseInt(n);
1688+
if(n<2 || n>32) throw "invalid radix";
1689+
return n;
1690+
}
16801691
function array_base2base(a,baseFrom,baseTo){//convert arrays of numerals from one base to another - implements support for Base64
16811692
var params=array_prepareEncodings(a,baseFrom,baseTo);//resolves any encodings before regular numeral conversions.
16821693
a=params[0];//our prep function returns the parameters as an array after it's done, let's get them back where they need to be.
@@ -1792,6 +1803,7 @@
17921803
<li><input type="radio" name="mode" value="9" id="rad9"><label for="rad9">Base64</label></li>
17931804
<li><input type="radio" name="mode" value="10" id="rad10"><label for="rad10">Ascii85</label></li>
17941805
<li><input type="radio" name="mode" value="1" id="rad1"><label for="rad1">Morse</label></li>
1806+
<li><input type="radio" name="mode" value="14" id="rad14"><label for="rad14">Radix-N</label></li>
17951807
</ul>
17961808
<div id="border"></div>
17971809
<div class="toolopen"><img src="img/tools.png" onclick="tbox_toggle()" alt="tools" title="text tools"></div>

dist/single/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<li><input type="radio" name="mode" value="9" id="rad9"><label for="rad9">Base64</label></li>
3535
<li><input type="radio" name="mode" value="10" id="rad10"><label for="rad10">Ascii85</label></li>
3636
<li><input type="radio" name="mode" value="1" id="rad1"><label for="rad1">Morse</label></li>
37+
<li><input type="radio" name="mode" value="14" id="rad14"><label for="rad14">Radix-N</label></li>
3738
</ul>
3839
<div id="border"></div>
3940
<div class="toolopen"><img src="img/tools.png" onclick="tbox_toggle()" alt="tools" title="text tools"></div>

dist/single/jxlate_single.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function foc(s){
6060
if(typeof addcredits == 'function') addcredits("ui.js",2.0,"crashdemons","JXlate UI script and overarching operations.")
6161
var oForm=null;
6262
var mode=0;
63-
var mode_bases=[256,'mc',2,8,10,16,'32r','32h','32c',64,85,'ue','ucs2','utf8'];//values used internally to represent each base in shorthand.
63+
var mode_bases=[256,'mc',2,8,10,16,'32r','32h','32c',64,85,'ue','ucs2','utf8','n'];//values used internally to represent each base in shorthand.
6464
//var mode_names=['Text','Morse','Binary','Octal','Decimal','Hexadecimal','Base32Rfc','Base32Hex','Base32Ckr','Base64','Ascii85','UrlEncode'];//unused array
6565

6666
function xlate_bytesNF(s,baseFrom){//translate a string from one base to another
@@ -102,7 +102,9 @@ function xlate_poll(){//poll the UI for mode radio-box changes.
102102
xlate_switch(oldmode,newmode);//trigger a translation
103103
}catch(e){
104104
setMode(oldmode);
105-
alert("This value could not be converted as specified.\nPlease make sure it is valid.")
105+
alert("This value could not be converted as specified.\nPlease make sure it is valid.\n\n"
106+
+"Technical Reason: \n"+" "+e
107+
)
106108
}
107109
console.log("conversion complete");
108110
}
@@ -1508,7 +1510,7 @@ fill_bases();
15081510
function fill_bases(){ for(var b=32;b>=0;b--) base_charsets[b]=b32hex.substr(0,b).split(""); }//set some charset options for use in conversion functions.
15091511

15101512
//check if the base ID is a numeral conversion or an encoding (external function)
1511-
function isEncodedBase(base){ return (base==="32r" ||base==="32h" ||base==="32c" || base===64 || base===85 || base==="mc" || base==="ue" || base==="ucs2" || base==="utf8");}
1513+
function isEncodedBase(base){ return (base==="32r" ||base==="32h" ||base==="32c" || base===64 || base===85 || base==="mc" || base==="ue" || base==="ucs2" || base==="utf8" || base==="n");}
15121514

15131515

15141516
//resolves any encodings before regular numeral conversions.
@@ -1528,6 +1530,7 @@ function array_prepareEncodings(a,baseFrom,baseTo){
15281530
else if(baseFrom==="mc" ) s=morse_decode(a);
15291531
else if(baseFrom==="ucs2") s=convert_encoding(a[0],'ucs2','iso88591');
15301532
else if(baseFrom==="utf8") s=convert_encoding(a[0],'utf8','iso88591');
1533+
else if(baseFrom==="n") s=array_base2base(a,radix_prompt(),256).join("");
15311534
a=s.split("");//decode the single BaseX entry into chars (base256)
15321535
baseFrom=256;//set up the parameter for the char->numeral array conversion.
15331536
}
@@ -1543,11 +1546,19 @@ function array_prepareEncodings(a,baseFrom,baseTo){
15431546
else if(baseTo==="mc" ) a=[ morse_encode(s)];
15441547
else if(baseTo==="ucs2") a=[convert_encoding(s,'iso88591','ucs2')];
15451548
else if(baseTo==="utf8") a=[convert_encoding(s,'iso88591','utf8')];
1549+
else if(baseTo==="n") a=array_base2base(a,256,radix_prompt());
15461550
baseFrom=baseTo;//we've encoded this to the new base, so lets set From to the current state - which disables any base conversion in array_base2base
15471551
}
15481552

15491553
return [a,baseFrom,baseTo];//output modified parameters.
15501554
}
1555+
function radix_prompt(){
1556+
var n = prompt("Please enter the radix (base) to convert with. (only 2-32 supported)", "");
1557+
if (n === null) throw "no entry";
1558+
n=parseInt(n);
1559+
if(n<2 || n>32) throw "invalid radix";
1560+
return n;
1561+
}
15511562
function array_base2base(a,baseFrom,baseTo){//convert arrays of numerals from one base to another - implements support for Base64
15521563
var params=array_prepareEncodings(a,baseFrom,baseTo);//resolves any encodings before regular numeral conversions.
15531564
a=params[0];//our prep function returns the parameters as an array after it's done, let's get them back where they need to be.

src/js/ui.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
if(typeof addcredits == 'function') addcredits("ui.js",2.0,"crashdemons","JXlate UI script and overarching operations.")
33
var oForm=null;
44
var mode=0;
5-
var mode_bases=[256,'mc',2,8,10,16,'32r','32h','32c',64,85,'ue','ucs2','utf8'];//values used internally to represent each base in shorthand.
5+
var mode_bases=[256,'mc',2,8,10,16,'32r','32h','32c',64,85,'ue','ucs2','utf8','n'];//values used internally to represent each base in shorthand.
66
//var mode_names=['Text','Morse','Binary','Octal','Decimal','Hexadecimal','Base32Rfc','Base32Hex','Base32Ckr','Base64','Ascii85','UrlEncode'];//unused array
77

88
function xlate_bytesNF(s,baseFrom){//translate a string from one base to another
@@ -44,7 +44,9 @@ function xlate_poll(){//poll the UI for mode radio-box changes.
4444
xlate_switch(oldmode,newmode);//trigger a translation
4545
}catch(e){
4646
setMode(oldmode);
47-
alert("This value could not be converted as specified.\nPlease make sure it is valid.")
47+
alert("This value could not be converted as specified.\nPlease make sure it is valid.\n\n"
48+
+"Technical Reason: \n"+" "+e
49+
)
4850
}
4951
console.log("conversion complete");
5052
}

src/js/xlate.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fill_bases();
88
function fill_bases(){ for(var b=32;b>=0;b--) base_charsets[b]=b32hex.substr(0,b).split(""); }//set some charset options for use in conversion functions.
99

1010
//check if the base ID is a numeral conversion or an encoding (external function)
11-
function isEncodedBase(base){ return (base==="32r" ||base==="32h" ||base==="32c" || base===64 || base===85 || base==="mc" || base==="ue" || base==="ucs2" || base==="utf8");}
11+
function isEncodedBase(base){ return (base==="32r" ||base==="32h" ||base==="32c" || base===64 || base===85 || base==="mc" || base==="ue" || base==="ucs2" || base==="utf8" || base==="n");}
1212

1313

1414
//resolves any encodings before regular numeral conversions.
@@ -28,6 +28,7 @@ function array_prepareEncodings(a,baseFrom,baseTo){
2828
else if(baseFrom==="mc" ) s=morse_decode(a);
2929
else if(baseFrom==="ucs2") s=convert_encoding(a[0],'ucs2','iso88591');
3030
else if(baseFrom==="utf8") s=convert_encoding(a[0],'utf8','iso88591');
31+
else if(baseFrom==="n") s=array_base2base(a,radix_prompt(),256).join("");
3132
a=s.split("");//decode the single BaseX entry into chars (base256)
3233
baseFrom=256;//set up the parameter for the char->numeral array conversion.
3334
}
@@ -43,11 +44,19 @@ function array_prepareEncodings(a,baseFrom,baseTo){
4344
else if(baseTo==="mc" ) a=[ morse_encode(s)];
4445
else if(baseTo==="ucs2") a=[convert_encoding(s,'iso88591','ucs2')];
4546
else if(baseTo==="utf8") a=[convert_encoding(s,'iso88591','utf8')];
47+
else if(baseTo==="n") a=array_base2base(a,256,radix_prompt());
4648
baseFrom=baseTo;//we've encoded this to the new base, so lets set From to the current state - which disables any base conversion in array_base2base
4749
}
4850

4951
return [a,baseFrom,baseTo];//output modified parameters.
5052
}
53+
function radix_prompt(){
54+
var n = prompt("Please enter the radix (base) to convert with. (only 2-32 supported)", "");
55+
if (n === null) throw "no entry";
56+
n=parseInt(n);
57+
if(n<2 || n>32) throw "invalid radix";
58+
return n;
59+
}
5160
function array_base2base(a,baseFrom,baseTo){//convert arrays of numerals from one base to another - implements support for Base64
5261
var params=array_prepareEncodings(a,baseFrom,baseTo);//resolves any encodings before regular numeral conversions.
5362
a=params[0];//our prep function returns the parameters as an array after it's done, let's get them back where they need to be.

src/template.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<li><input type="radio" name="mode" value="9" id="rad9"><label for="rad9">Base64</label></li>
3535
<li><input type="radio" name="mode" value="10" id="rad10"><label for="rad10">Ascii85</label></li>
3636
<li><input type="radio" name="mode" value="1" id="rad1"><label for="rad1">Morse</label></li>
37+
<li><input type="radio" name="mode" value="14" id="rad14"><label for="rad14">Radix-N</label></li>
3738
</ul>
3839
<div id="border"></div>
3940
<div class="toolopen"><img src="img/tools.png" onclick="tbox_toggle()" alt="tools" title="text tools"></div>

0 commit comments

Comments
 (0)