Skip to content

Commit

Permalink
Corrected an issue were numerals were preceeded with 0 even when thei…
Browse files Browse the repository at this point in the history
…r charset starts with another char. Corrected Radix-N support using intermediate byte conversion leading to incorrect results - these are now converted directly like normal modes. Removed the error message for Radix-N when you hit cancel. Fixed Radix-N support for charsets with letters, a casing issue prevented them from being interpreted correctly.
  • Loading branch information
crashdemons committed Jun 17, 2015
1 parent 261b3d2 commit fbe1091
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 68 deletions.
2 changes: 1 addition & 1 deletion dist/full/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<script src="js/xlate.js"></script>

<script>
if(typeof addcredits == 'function') addcredits("jxlate.html",2.2,"crashdemons","JavaScript Binary Translator UI.")
if(typeof addcredits == 'function') addcredits("jxlate.html",2.3,"crashdemons","JavaScript Binary Translator UI.")
</script>
</head>
<body class="monospace bw" onload="xlate_init()">
Expand Down
9 changes: 5 additions & 4 deletions dist/full/js/ui.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* @P2@ */
if(typeof addcredits == 'function') addcredits("ui.js",2.0,"crashdemons","JXlate UI script and overarching operations.")
if(typeof addcredits == 'function') addcredits("ui.js",2.1,"crashdemons","JXlate UI script and overarching operations.")
var oForm=null;
var mode=0;
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.
Expand Down Expand Up @@ -44,9 +44,10 @@ function xlate_poll(){//poll the UI for mode radio-box changes.
xlate_switch(oldmode,newmode);//trigger a translation
}catch(e){
setMode(oldmode);
alert("This value could not be converted as specified.\nPlease make sure it is valid.\n\n"
+"Technical Reason: \n"+" "+e
)
if(e!=="no entry")
alert("This value could not be converted as specified.\nPlease make sure it is valid.\n\n"
+"Technical Reason: \n"+" "+e
)
}
console.log("conversion complete");
}
Expand Down
4 changes: 2 additions & 2 deletions dist/full/js/xlate.format.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if(typeof addcredits == 'function') addcredits("xlate.format.js",10,"crashdemons","Numeral System formatting functions")
if(typeof addcredits == 'function') addcredits("xlate.format.js",11,"crashdemons","Numeral System formatting functions")

//preformatter function that changes and splits the text to an array of single units for array_base2base to translate.
//non-numeral strings and encodings tend to have just a single item.
Expand All @@ -21,7 +21,7 @@ function input2buffer(s,base){
if(s.substr(-2,2)!="~>") s+="~>";
return [s];//single item to pass to decoder.
}
return s.split(" ");//all other items are split by spaces.
return s.toUpperCase().split(" ");//all other items are split by spaces.
}

//postformatter function that joins and changes the output from array_base2base to be readable.
Expand Down
32 changes: 22 additions & 10 deletions dist/full/js/xlate.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
if(typeof addcredits === 'function') addcredits("xlate.js",9,"crashdemons","Binary Translation library for numeral systems (extensions added for encoding libraries)")
if(typeof addcredits === 'function') addcredits("xlate.js",10,"crashdemons","Binary Translation library for numeral systems (extensions added for encoding libraries)")

var b32hex="0123456789ABCDEFGHIJKLMNOPQRSTUV";//Triacontakaidecimal
var b32hex="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//Triacontakaidecimal+4
var base_charsets=[];
fill_bases();


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.
function fill_bases(){
for(var b=36;b>=0;b--) base_charsets[b]=b32hex.substr(0,b).split("");
base_charsets[26]="ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
}//set some charset options for use in conversion functions.

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


//resolves any encodings before regular numeral conversions.
Expand All @@ -18,6 +21,10 @@ function array_prepareEncodings(a,baseFrom,baseTo){
if( (!fromEncoded) && (!toEncoded) ) return [a,baseFrom,baseTo];//if the input unit array is neither encoded nor being encoded, just return it to array_base2base for numeral conversion.
var s="";





if(fromEncoded){//catch all of the encoded strings coming in that need to be decoded before translation.
if( baseFrom===64 ) s=atob(a[0]);
else if(baseFrom==="ue" ) s=unescape(a[0]);//I know this is deprecated, but decodeURI does not do what I need.
Expand All @@ -28,7 +35,7 @@ function array_prepareEncodings(a,baseFrom,baseTo){
else if(baseFrom==="mc" ) s=morse_decode(a);
else if(baseFrom==="ucs2") s=convert_encoding(a[0],'ucs2','iso88591');
else if(baseFrom==="utf8") s=convert_encoding(a[0],'utf8','iso88591');
else if(baseFrom==="n") s=array_base2base(a,radix_prompt(),256).join("");
//else if(baseFrom==="n") s=array_base2base(a,radix_prompt(),256).join("");
a=s.split("");//decode the single BaseX entry into chars (base256)
baseFrom=256;//set up the parameter for the char->numeral array conversion.
}
Expand All @@ -44,25 +51,29 @@ function array_prepareEncodings(a,baseFrom,baseTo){
else if(baseTo==="mc" ) a=[ morse_encode(s)];
else if(baseTo==="ucs2") a=[convert_encoding(s,'iso88591','ucs2')];
else if(baseTo==="utf8") a=[convert_encoding(s,'iso88591','utf8')];
else if(baseTo==="n") a=array_base2base(a,256,radix_prompt());
//else if(baseTo==="n") a=array_base2base(a,256,radix_prompt());
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
}

return [a,baseFrom,baseTo];//output modified parameters.
}
function radix_prompt(){
var n = prompt("Please enter the radix (base) to convert with. (only 2-32 supported)", "");
var n = prompt("Please enter the radix (base) to convert with. (only 2-36 supported)", "");
if (n === null) throw "no entry";
n=parseInt(n);
if(n<2 || n>32) throw "invalid radix";
if(n<2 || n>36) throw "invalid radix";
return n;
}
function array_base2base(a,baseFrom,baseTo){//convert arrays of numerals from one base to another - implements support for Base64
if(baseFrom==="n") baseFrom=radix_prompt();
if(baseTo ==="n") baseTo =radix_prompt();

var params=array_prepareEncodings(a,baseFrom,baseTo);//resolves any encodings before regular numeral conversions.
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.
baseFrom=params[1];
baseTo=params[2];


if(baseFrom===baseTo) return a;//the from and to bases are the same - no conversion necessary!
for(var i=0,len=a.length;i<len;i++) a[i]=base2base(a[i], baseFrom, baseTo);//do a base conversion on each array element (unit in the From Base) to the To base.
return a;
Expand Down Expand Up @@ -116,11 +127,12 @@ function dec2numeral(d,sbase){
var snum="";
while(d>0){
var rem=d%base;//find last digit value of numeral
var sdig=base_charsets[sbase][rem];//char representation of the digit
var sdig=base_charsets[base][rem];//char representation of the digit
d=Math.floor(d/base);//remove the last numeral digit from the integer (value-wise)
snum=sdig+snum;//prepend because the last digit we process will be the most significant.
}
return (snum === '') ? "0" : snum;
console.log(sbase +" "+base);
return (snum === '') ? base_charsets[base][0] : snum;
}


Expand Down
47 changes: 30 additions & 17 deletions dist/monolithic/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
return foa
}
/* @P2@ */
if(typeof addcredits == 'function') addcredits("ui.js",2.0,"crashdemons","JXlate UI script and overarching operations.")
if(typeof addcredits == 'function') addcredits("ui.js",2.1,"crashdemons","JXlate UI script and overarching operations.")
var oForm=null;
var mode=0;
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.
Expand Down Expand Up @@ -231,9 +231,10 @@
xlate_switch(oldmode,newmode);//trigger a translation
}catch(e){
setMode(oldmode);
alert("This value could not be converted as specified.\nPlease make sure it is valid.\n\n"
+"Technical Reason: \n"+" "+e
)
if(e!=="no entry")
alert("This value could not be converted as specified.\nPlease make sure it is valid.\n\n"
+"Technical Reason: \n"+" "+e
)
}
console.log("conversion complete");
}
Expand Down Expand Up @@ -1567,7 +1568,7 @@

document.Util_Loaded = 1;

if(typeof addcredits == 'function') addcredits("xlate.format.js",10,"crashdemons","Numeral System formatting functions")
if(typeof addcredits == 'function') addcredits("xlate.format.js",11,"crashdemons","Numeral System formatting functions")

//preformatter function that changes and splits the text to an array of single units for array_base2base to translate.
//non-numeral strings and encodings tend to have just a single item.
Expand All @@ -1590,7 +1591,7 @@
if(s.substr(-2,2)!="~>") s+="~>";
return [s];//single item to pass to decoder.
}
return s.split(" ");//all other items are split by spaces.
return s.toUpperCase().split(" ");//all other items are split by spaces.
}

//postformatter function that joins and changes the output from array_base2base to be readable.
Expand Down Expand Up @@ -1629,17 +1630,20 @@
//return ( isReadable(a) || (a>=0xA0 && a<=0xFF));
}

if(typeof addcredits === 'function') addcredits("xlate.js",9,"crashdemons","Binary Translation library for numeral systems (extensions added for encoding libraries)")
if(typeof addcredits === 'function') addcredits("xlate.js",10,"crashdemons","Binary Translation library for numeral systems (extensions added for encoding libraries)")

var b32hex="0123456789ABCDEFGHIJKLMNOPQRSTUV";//Triacontakaidecimal
var b32hex="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//Triacontakaidecimal+4
var base_charsets=[];
fill_bases();


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.
function fill_bases(){
for(var b=36;b>=0;b--) base_charsets[b]=b32hex.substr(0,b).split("");
base_charsets[26]="ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
}//set some charset options for use in conversion functions.

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


//resolves any encodings before regular numeral conversions.
Expand All @@ -1649,6 +1653,10 @@
if( (!fromEncoded) && (!toEncoded) ) return [a,baseFrom,baseTo];//if the input unit array is neither encoded nor being encoded, just return it to array_base2base for numeral conversion.
var s="";





if(fromEncoded){//catch all of the encoded strings coming in that need to be decoded before translation.
if( baseFrom===64 ) s=atob(a[0]);
else if(baseFrom==="ue" ) s=unescape(a[0]);//I know this is deprecated, but decodeURI does not do what I need.
Expand All @@ -1659,7 +1667,7 @@
else if(baseFrom==="mc" ) s=morse_decode(a);
else if(baseFrom==="ucs2") s=convert_encoding(a[0],'ucs2','iso88591');
else if(baseFrom==="utf8") s=convert_encoding(a[0],'utf8','iso88591');
else if(baseFrom==="n") s=array_base2base(a,radix_prompt(),256).join("");
//else if(baseFrom==="n") s=array_base2base(a,radix_prompt(),256).join("");
a=s.split("");//decode the single BaseX entry into chars (base256)
baseFrom=256;//set up the parameter for the char->numeral array conversion.
}
Expand All @@ -1675,25 +1683,29 @@
else if(baseTo==="mc" ) a=[ morse_encode(s)];
else if(baseTo==="ucs2") a=[convert_encoding(s,'iso88591','ucs2')];
else if(baseTo==="utf8") a=[convert_encoding(s,'iso88591','utf8')];
else if(baseTo==="n") a=array_base2base(a,256,radix_prompt());
//else if(baseTo==="n") a=array_base2base(a,256,radix_prompt());
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
}

return [a,baseFrom,baseTo];//output modified parameters.
}
function radix_prompt(){
var n = prompt("Please enter the radix (base) to convert with. (only 2-32 supported)", "");
var n = prompt("Please enter the radix (base) to convert with. (only 2-36 supported)", "");
if (n === null) throw "no entry";
n=parseInt(n);
if(n<2 || n>32) throw "invalid radix";
if(n<2 || n>36) throw "invalid radix";
return n;
}
function array_base2base(a,baseFrom,baseTo){//convert arrays of numerals from one base to another - implements support for Base64
if(baseFrom==="n") baseFrom=radix_prompt();
if(baseTo ==="n") baseTo =radix_prompt();

var params=array_prepareEncodings(a,baseFrom,baseTo);//resolves any encodings before regular numeral conversions.
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.
baseFrom=params[1];
baseTo=params[2];


if(baseFrom===baseTo) return a;//the from and to bases are the same - no conversion necessary!
for(var i=0,len=a.length;i<len;i++) a[i]=base2base(a[i], baseFrom, baseTo);//do a base conversion on each array element (unit in the From Base) to the To base.
return a;
Expand Down Expand Up @@ -1747,11 +1759,12 @@
var snum="";
while(d>0){
var rem=d%base;//find last digit value of numeral
var sdig=base_charsets[sbase][rem];//char representation of the digit
var sdig=base_charsets[base][rem];//char representation of the digit
d=Math.floor(d/base);//remove the last numeral digit from the integer (value-wise)
snum=sdig+snum;//prepend because the last digit we process will be the most significant.
}
return (snum === '') ? "0" : snum;
console.log(sbase +" "+base);
return (snum === '') ? base_charsets[base][0] : snum;
}


Expand All @@ -1778,7 +1791,7 @@

</script>
<script>
if(typeof addcredits == 'function') addcredits("jxlate.html",2.2,"crashdemons","JavaScript Binary Translator UI.")
if(typeof addcredits == 'function') addcredits("jxlate.html",2.3,"crashdemons","JavaScript Binary Translator UI.")
</script>
</head>
<body class="monospace bw" onload="xlate_init()">
Expand Down
2 changes: 1 addition & 1 deletion dist/single/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<link rel="stylesheet" type="text/css" href="jxlate.css">
<script src="jxlate_single.js"></script>
<script>
if(typeof addcredits == 'function') addcredits("jxlate.html",2.2,"crashdemons","JavaScript Binary Translator UI.")
if(typeof addcredits == 'function') addcredits("jxlate.html",2.3,"crashdemons","JavaScript Binary Translator UI.")
</script>
</head>
<body class="monospace bw" onload="xlate_init()">
Expand Down
Loading

0 comments on commit fbe1091

Please sign in to comment.