Compress large Integers into smallest possible string

后端 未结 6 1609
夕颜
夕颜 2020-12-08 21:04

I have a bunch of 10 digit integers that I\'m passing in a URL. Something like: \"4294965286\", \"2292964213\". They will always be positive and always be 10 digits.

6条回答
  •  臣服心动
    2020-12-08 21:47

    I think what you're looking for are Hash IDs: http://hashids.org/

    They have implementations in many languages, although it looks like C# is not one of them.

    I made an example for you in JavaScript: http://codepen.io/codycraven/pen/MbWwQm

    var hashids = new Hashids('my salt', 1, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890');
    var input = 4294965286;
    var hex = input.toString(16); // 8 characters: fffff826
    var hashid = hashids.encode(input); // 7 characters: 0LzaR1Y
    var base64 = window.btoa(input).replace(/=+/, ''); // 14 characters: NDI5NDk2NTI4Ng
    

    Note that the HashIDs libraries protect your hashes from including foul language.

提交回复
热议问题