preface

When we write code at ordinary times will occasionally encounter the problem of base conversion, common have 2 base, 8 base, 10 base, between the conversion of 16 base, but 36 base but rarely heard of, here let us use JS to simply try the implementation of 36 base

thinking

The composition of 36 base numbers

Before we start, we need to understand the 36-base numbers so that we can have a clear understanding of the 36-base numbers and the subsequent conversion of numbers. We all know that hexadecimal numbers are made up of characters between 0 and 9 and a and F, but what about 36? If you continue a-f by 20 places, it is obvious that the letter part is made up of exactly the 26 letters a-Z, so the 36-base number is 0-9, a-z

Numerical preparation

After figuring out its composition, I began to think, how can a base 10 number be transformed into a base 36 number? We need to prepare a “warehouse” there are 36 numerical array, the array used to store all the numerical hexadecimal number 36, when a decimal number to a binary conversion, according to the numerical value into the warehouse to take out the 36 hexadecimal values, the number in the warehouse, 0 to 9 on behalf of 36 hexadecimal number 0-9, 10-35 36 hexadecimal number representing a to z. The following code

function getNums36() {
  var nums36 = [];
  for(var i = 0; i < 36 ; i++) {
    if(i >= 0 && i <= 9) { // Store the values from 0 to 9
      nums36.push(i) 
    } else {  // Store a-z values
      nums36.push(String.fromCharCode(i + 87));  // ASCII conversion}}console.log(nums36,'-- -- -- -- -- -- -- --);  // Check the value of the warehouse
  return nums36; 
}

Copy the code

implementation

Once the warehouse is built, we begin to decompose the base conversion process.

  1. The decimal number passed in is detected first, and the floating-point number is judged first. Since floating-point conversion is not discussed here, it is returned directly. Second, negative numbers are detected and processed, and if n is negative, math.abs () is called to convert n to a positive number.

  2. After the detection is complete, the transformation begins

Set up a while loop, inside the while, do the mod to n of 36 to get res,

var res = n % 36;

To get the lowest value of the 10 to 36 base number, throw the RES into the store, get the 36 base number, and use unshift to store the arR first.

arr.unshift(nums36[res]);

After the least significant number is processed, we need to carry the number and process the higher digit values

n = parseInt(n/36);
Copy the code

At this point, the loop is complete

We use while to mod n res and carry. Finally, we can convert a base 10 to a base 36

Note that at this point, remember to add the negative value of neg to the first digit

arr.unshift(neg)
Copy the code

Finally, return the 36 base number

return arr.join("");
Copy the code

code

// Provide 36 bits of expression 0-9 a-z
function getNums36() {
  var nums36 = [];
  for(var i = 0; i < 36 ; i++) {
    if(i >= 0 && i <= 9) {
      nums36.push(i)
    } else {
      nums36.push(String.fromCharCode(i + 87)); }}return nums36;
}
function scale36(n) {
  // Separate function functions
  // Hexadecimal numbers: 0-9 a-f 36 base numbers: 0-9 a-z
  const arr = [];
  var nums36 = getNums36();
  / / 36 10
  if(!Number.isInteger(n)){// The mouse is not supported at present
    console.warn('Decimal conversion not supported');
    return n;
  } 
  var neg = ' ';
  if(n < 0) {// Handle negative numbers
      neg = The '-';
      n = Math.abs(n)
  }
  while(n) {
    var res = n % 36;
    console.log(res,'+ + + + + + +');
    arr.unshift(nums36[res]);
    / / carry
    n = parseInt(n/36);
    console.log(n,'-- -- -- -- -- -- -- -- --');
  }
  arr.unshift(neg)
  return arr.join("");

}

console.log(scale36(20)); / / 10
Copy the code

extension

The realization of base 7 numbers

This template is also suitable for conversion of base 10 to other bases, we just need to change the value of the warehouse, here is a LeetCode base 7 problem as an example

504. Seven base numbers

Given an integer, convert it to base 7 and print it as a string.

Example 1:

Input: 100 Output: “202”

Prepare a value warehouse for base 7 numbers

function getNums7() {
  var nums7 = [];
  for(var i = 0; i < 7 ; i++) {
    
      nums7.push(i)
   
  }
  return nums7;
}
Copy the code

Then modify the value of the mod and carry, you can complete the template reuse

var res = n % 7;
n = parseInt(n/7);
Copy the code

code


function getNums7() {
  var nums7 = [];
  for(var i = 0; i < 7 ; i++) {
    
      nums7.push(i)
   
  }
  return nums7;
}
var convertToBase7 = function(num) {
  // Separate function functions
  
  const arr = [];
  var nums7 = getNums7();
  var neg = ' ';
  if(num < 0) {// Handle negative numbers
      neg = The '-';
      num = Math.abs(num)
  }
  if(num == 0) {
      return  num + "";
  }
  
  while(num) {
    var res = num % 7;  // Intercept the high level data
    arr.unshift(nums7[res]);  
    / / carry
    num = parseInt(num/7); 
  }
  arr.unshift(neg);
  return arr.join("");

}
Copy the code

summary

As can be seen from this example, with a little modification, we can complete arbitrary conversion from base 10 to other bases. The core lies in the mod of RES and the construction of warehouse values. We can finally get the base number we want through constant carry and while loop