2

I have a base64 string like this = "pCZXOVjpnlePyDk6znZrSw==" i need a encoder/decoder to convert it to decimal string like this = "84587163248712923874"
Is there any codec that do it?

5
  • Language, platform, context? It's also unclear what digits those are meant to be... please give more information.
    – Jon Skeet
    Sep 23, 2011 at 11:31
  • encode/decode base64 to a string that contain only digits. java Sep 23, 2011 at 11:34
  • That base 64 string decodes to the 16 bytes A426573958E99E578FC8393ACE766B4B whats their relationship to the number?
    – Alex K.
    Sep 23, 2011 at 11:39
  • base64 is base64. However, the encoder works only on bytes, so you must convert your characters to bytes before encoding, and convert back after decoding.
    – Hot Licks
    Sep 23, 2011 at 11:44
  • stackoverflow.com/questions/469695/decode-base64-data-in-java tells you how to decode a base64 string, as for what you get back; if its an encoded decimal string that's what you will get, if its a picture of a cat, then you'll get that.
    – Alex K.
    Sep 23, 2011 at 11:45

2 Answers 2

2

You probably need a two stage process:

  1. Convert the Base64 string to a byte array.
  2. Convert the byte array to a number.

16 bytes is too big for a Java long so I assume that you will need a BigInteger. There is a BigInteger constructor that takes a byte array as parameter, though you will need to be careful with the sign bit. The toString() method for the BigInteger will give you the string you want.

2

Base64 encode/decode is part of the default libraries now (finally). Look at the class javax.xml.bind.DatatypeConverter. In there you will find two methods printBase64Binary and parseBase64Binary (along with a few other conversion routines).

1
  • This tells how to parse base64, but not how to convert the result (a byte array) to a decimal string.
    – LarsH
    May 17, 2018 at 0:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.