1

To encode a ByteArray to Base64 I could simply use Base64 from java.util. But now I need to change my code to create base36 instead. Unfortunately java.util doesn't have this functionality. What I need is a function/method that takes a ByteArray and outputs a String containing a base36 representation of it. No other changes like cutting off leading zeroes.

So this other question looks similar but we're are 2 problems. First the question got edited so that I don't understand the answer. Second, the answer uses BigInteger and I'm afraid that converting the ByteArray to a BigInteger could lead to information loss (like leading zeroes). Similar question on stackoverflow.

Yava
  • 123
  • 1
  • 1
  • 7
  • 1
    The difference is that 64 is an exact number of bits whereas 36 isn't. So you'll need to effectively repeatedly divide the whole byte array by 36, taking each remainder as a digit from the right-hand end - which is going to be a lot easier to just use BigInteger, You're probably right it would drop leading zeroes: do you need to preserve them? If you do maybe calculate how long you'd expect the output string to be, use the BigInteger method and then add any leading zeroes needed to make up the expected length. – Rup Sep 13 '20 at 23:54
  • Yes I need the leading zeroes because it's not a number. I'm not sure if I can guess the expected length, in the meantime I'm looking for an alternative way: base64 string to base36 string. I think that's suboptimal with an unnecessary step to base64 but maybe I find more solutions for this. – Yava Sep 14 '20 at 00:05
  • 1
    If it's not a number, why on earth would it be using Base36? It makes no sense, since Base36 cannot be done incrementally, as the bytes are streamed/processed. – Andreas Sep 14 '20 at 01:11
  • All I can say is that base36 is a binary to text encoding sheme, a binary can contain everything. – Yava Sep 14 '20 at 03:21
  • Off the top of my head, `base36digits = Math.ceil(bytes * Math.log(256) / Math.log(36))`. But base36 sounds like it's the wrong encoding to use here, so if you can find out why it's needed and switch to something else like base64 instead that would be sensible. – Rup Sep 14 '20 at 05:50
  • How do you multiplicate a bytearray? Or do you do the math on each byte? And why the magic number 256? – Yava Sep 22 '20 at 11:25
  • 256 = 2 to the power of 8 = the number of possible values of a byte. No, you don't multiply the array, just the length of the array i.e. the number of bytes you have, which is what I meant for the variable `bytes` in the calculation. – Rup Sep 22 '20 at 11:31
  • `Math.log(256) / Math.log(36)` is the number of base 36 digits you'll need to represent one byte of data, so we multiply that by the number of bytes you have and then round up to get the number of base 36 digits that you'd need. – Rup Sep 22 '20 at 11:31
  • As an alternative approach to getting the correct number of digits you could also generate a byte array of the same length that just contains the value 255 for every byte, load this into a BigInteger too and then as you generate the digits for your input array also divide this new array by 36 every time. When the BigInteger for this new array is zero then you have generated enough digits. – Rup Sep 22 '20 at 11:34
  • Or actually you're not dividing are you, you're just using BigInteger.toString()? Convert that second array to base 36 using BigInteger too and then pad your first value to the same number of base36 digits. – Rup Sep 22 '20 at 11:41

0 Answers0