Thamrill

Untitled

Feb 23rd, 2022 (edited)
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ConvertDigit(d) {
  2.   if (d < 10) {
  3.     return String.fromCharCode(48 + d);
  4.   }
  5.   if (d < 10 + 26) {
  6.     return String.fromCharCode(65 + d - 10);
  7.   }
  8.   if (d < 10 + 26 + 26) {
  9.     return String.fromCharCode(97 + d - 10 - 26);
  10.   }
  11.   if (d < 72) {
  12.     return String.fromCharCode(33 + d - 10 - 26 - 26);
  13.   }
  14. }
  15.  
  16. function ConvertChar(c) {
  17.     if(c>=48&&c<=57){
  18.     return c-48;
  19.   }
  20.   if(c>=65&&c<=90){
  21.     return c-65+10;
  22.   }
  23.   if(c>=97&&c<=122){
  24.     return c-97+10+26;
  25.   }
  26.   if(c>=33&&c<=43){
  27.     return c-33+10+26+26;
  28.   }
  29. }
  30.  
  31. function Num2Str(d) {
  32.   var magnitude;
  33.   if (d ===0) {
  34.     return "0";
  35.   } else {
  36.     magnitude = Math.ceil(Math.log(d + 1) / Math.log(72));
  37.   }
  38.   var value=d;
  39.     const array=[];
  40.   for(var ii=0; ii<magnitude;ii++){
  41.     var ex=magnitude-ii-1;
  42.     var val=Math.floor(value/Math.pow(72, ex));
  43.     value-=val*Math.pow(72, ex);
  44.     array[ii]=ConvertDigit(val);
  45.   }
  46.   return array.join("");
  47. }
  48.  
  49. function Str2Num(str) {
  50.     var l=str.length;
  51.   var value=0;
  52.   for(var ii=0; ii<l;ii++){
  53.     var ex=l-ii-1;
  54.     value+=ConvertChar(str.charCodeAt(ii))*Math.pow(72, ex);
  55.   }
  56.   return value;
  57. }
  58.  
  59. for(var ii=0; ii<1e2; ii++){
  60.   if(ii!=Str2Num(Num2Str(ii))){
  61.     console.log(ii+" "+Num2Str(ii)+" "+Str2Num(Num2Str(ii)));
  62.   }
  63. }
  64. console.log("Finished");
Add Comment
Please, Sign In to add comment