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?

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
Sajad Bahmani
  • 17,325
  • 27
  • 86
  • 108
  • Language, platform, context? It's also unclear what digits those are meant to be... please give more information. – Jon Skeet Sep 23 '11 at 11:31
  • encode/decode base64 to a string that contain only digits. java – Sajad Bahmani Sep 23 '11 at 11:34
  • That base 64 string decodes to the 16 bytes A426573958E99E578FC8393ACE766B4B whats their relationship to the number? – Alex K. Sep 23 '11 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 '11 at 11:44
  • http://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 '11 at 11:45

2 Answers2

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.

rossum
  • 15,344
  • 1
  • 24
  • 38
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).

LarsH
  • 27,481
  • 8
  • 94
  • 152
Jere
  • 31
  • 1
  • This tells how to parse base64, but not how to convert the result (a byte array) to a decimal string. – LarsH May 17 '18 at 00:24