sql >> Base de Datos >  >> NoSQL >> MongoDB

Identificación breve y fácil de usar para mongo

Está intentando convertir una base-16 (hexadecimal) a base-36 (26 caracteres en el alfabeto más 10 números). Una forma sencilla podría ser simplemente usar parseInt 's parámetro radix para analizar la identificación hexadecimal y luego llamar a .toString(36) para convertir eso en base-36. Lo que convertiría "507f191e810c19729de860ea" en "VDFGUZEA49X1V50356", reduciendo la longitud de 24 a 18 caracteres.

function toBase36(id) {
  var half = Math.floor(id.length / 2);
  var first = id.slice(0, half);
  var second = id.slice(half);
  return parseInt(first, 16).toString(36).toUpperCase()
       + parseInt(second, 16).toString(36).toUpperCase();
}

function toBase36(id) {
  var half = Math.floor(id.length / 2);
  var first = id.slice(0, half);
  var second = id.slice(half);
  return parseInt(first, 16).toString(36).toUpperCase()
       + parseInt(second, 16).toString(36).toUpperCase();
}

// Ignore everything below (for demo only)
function convert(e){ if (e.target.value.length % 2 === 0) base36.value = toBase36(e.target.value) }
var base36 = document.getElementById('base36');
var hex = document.getElementById('hex');
document.getElementById('hex').addEventListener('input', convert, false);
convert({ target: { value: hex.value } });
input { font-family: monospace; width: 15em; }
<input id="hex" value="507f191e810c19729de860ea">
<input id="base36" readonly>