Created
May 8, 2016 12:44
-
-
Save xemoe/eabbc3b2f9233a2c66a66a9150c48335 to your computer and use it in GitHub Desktop.
Base 26 convert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const bigInt = require("big-integer"); | |
function s26toi(s) { | |
const base = 26; | |
let ret = 0; | |
let numl = String(s).length; | |
let rev = 0; | |
for (let i = numl - 1; i >= 0; i--) { | |
let ton = bigInt(s.charAt(i).charCodeAt(0) - 97).multiply(bigInt(base).pow(rev)); | |
ret = bigInt(ret).add(ton); | |
rev++; | |
} | |
return bigInt(ret); | |
} | |
function itos26(num) { | |
const base = 26; | |
let ret = ""; | |
let next = true; | |
let i = 1; | |
while (next) { | |
let ton = parseInt(bigInt(num).mod(bigInt(base).pow(i)).divide(bigInt(base).pow(i-1)).toString()); | |
ret = `${b26(ton)}${ret}`; | |
next = bigInt(num).mod(Math.pow(base, i)) != num; | |
i++; | |
} | |
return ret; | |
} | |
function b26(num) { | |
return String.fromCharCode(97 + num); | |
} | |
exports.s26toi = s26toi; | |
exports.itos26 = itos26; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment