Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
711 views
in Technique[技术] by (71.8m points)

javascript - encode a big integer to base62

I need to create unique referral codes. To make each Referral code unique I am trying to do something like below.

const stringVal = `${currentTimestamp}${someVal}${someKey}`

Here someVal itself is BigInt mysql datatype, and someKey will always be two digit number.

To encode, I am using base62 package.

Whever I pass someVal more than 99, I start to get unexpected results.

function generateReferralCode(someVal) {
   const currentTimestamp = Date.now()
   let someKey = getSomeKey(someVal)
   const val = `${currentTimestamp}${someVal}${someKey}`
   console.log(`number to string : ${val}`)
   console.log(`longed value from string : ${Long.fromString(val)}`)
   const encodedVal = await base62.encode(Long.fromString(val))
   return encodedVal
}
function parseReferralCode(referralCode) {
    const decodedVal = base62.decode(referralCode)
    console.log(`decoded number : ${decodedVal}`)
    //extract someValue
    let  somevalue = parseInt(decodedVal / 100) % ( Math.pow(10, (decodedVal % 100)))
    return someValue
}

async function test() {
    const encoded = await generateReferralCode(100)
    console.log(`encoded val: ${encoded}`)
    const decoded = await parseReferralCode(encoded)
    console.log(`decoded val: ${decoded}`)
}
test()

Output:

number to  string : '158632196111710003'
longed value from string : 158632196111710003
encoded val: 'bIxiLMdWrm'
decoded number : 158632196111710000 //this should be as encoded string or longed val
decoded val: 1586321961117100 //this should be 100

The above doesn't work for value of someValue > 99. I know it's because of integer length in javascript.

I also tried using long, but it is also not working.

I think base62 package also don't support big integers.

Can someone help how can I base62 encode that kind of big numbers?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

FWIW, the following are two helper functions that convert a BigInt to a desired base and vice versa...

var digits = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

function bigint2base( x, baseDigits ) {

  let base = BigInt( baseDigits.length );
  let result = '';
  
  while ( 0 < x ) {
    result = baseDigits.charAt( Number( x % base ) ) + result;
    x = x / base;
  }
    
  return result || '0';
  
}


function base2bigint( x, baseDigits ) {

  let base = BigInt( baseDigits.length ); 
  let result = 0n;
  
  for ( let i = 0; i < x.length; i++ ) {
    result = result * base + BigInt( baseDigits.indexOf( x.charAt( i ) ) );
  }
  
  return result;
}

console.log( `BigInt 158632196111710003n to base62 ( '${digits}' )...`);
base62val = bigint2base( 158632196111710003n, digits );
console.log( base62val );

console.log( `Converted back to BigInt is...` );
console.log( base2bigint( base62val, digits ).toString() );

console.log('');
console.log( `BigInt 31957108571341948394931814703184314387417471347093174n to base62 ( '${digits}' )...`);
base62val = bigint2base( 31957108571341948394931814703184314387417471347093174n, digits );
console.log( base62val );

console.log( `Converted back to BigInt is...` );
console.log( base2bigint( base62val, digits ).toString() );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...