Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't convert to Base64 #365

Closed
rafaelaznar opened this issue Jan 12, 2024 · 1 comment
Closed

Can't convert to Base64 #365

rafaelaznar opened this issue Jan 12, 2024 · 1 comment

Comments

@rafaelaznar
Copy link

Can't convert to Base64 beacuse in Alphabet in B64 contains symbols that can't pass the condition /^.$|[+-.\s]|(.).*\1/ giving

Uncaught Error: [BigNumber Error] ALPHABET invalid: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
    at _.config._.set (bignumber.min.js:2:6185)

Any workaround?

@MikeMcl
Copy link
Owner

MikeMcl commented Jan 23, 2024

It would just be a matter of replacing the characters. For example:

BigNumber.set({ 
  ALPHABET: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_'
});

const bigNumberToBase64 = big => big.toString(64).
  replace(/\$/g, '+').replace(/_/g, '-');

const bigNumberFromBase64 = str => new BigNumber(
  str.replace(/\+/g, '$').replace(/\-/g, '_'), 64);

console.log(
  bigNumberToBase64(bigNumberFromBase64('+123abcZ+--'))
);
// '+123abcZ+--'

@MikeMcl MikeMcl closed this as completed Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants