Sajad Bahmani
Sajad Bahmani

Reputation: 17469

encode and decode base64 string to decimal string coddec

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?

Upvotes: 2

Views: 3716

Answers (2)

Jere
Jere

Reputation: 31

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).

Upvotes: 2

rossum
rossum

Reputation: 15693

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.

Upvotes: 2

Related Questions