Is it possible to base 36 encode with JavaScript / jQuery?

后端 未结 4 2082
臣服心动
臣服心动 2021-01-31 18:00

I\'m thinking about using the encode/decode technique here (Encoding to base 36/decoding from base 36 is simple in Ruby)

how to implement a short url like urls in twitte

4条回答
  •  青春惊慌失措
    2021-01-31 18:24

    I wrote this baseXY shortener, designed for a very similar question/need I had:

    https://github.com/marko-36/base29-shortener.

    • By default, in order to leave out easily interchangeable characters, it is a base29 shortener, but the 'XY' number is the same as number of characters you decide to use for encoding.
    • Since the set of characters is fully customizable, you can omit any you wish (i, l, 1, L, o, 0, O..)
    • To make it less obvious, what the encoding character set is, and/or if you want the encoding string to have a minimal length, just start encoding from a high enough number.

    This is it, hope it helps:

        const c=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '2', '3', '4', '5', '6', '7', '8', '9']; 
        //characters for encoding
    
        function to29(i){
            var sLen = Math.floor(Math.log(i)/Math.log(c.length)) +1;
            var s = '';
            for(ex=sLen-1; ex>-1; --ex){
                s += c[Math.floor(i / Math.pow(c.length,ex))];
                i = [i % Math.pow(c.length,ex)];
            }
            return s;
        }
    
        function from29(s){
            var i = 0;
            for (ex=0; ex

提交回复
热议问题