2

I am surfing around from quite some time for a proper solution for the above question.

I could not find the solution for the conversion/encoding in Java language.

I need to encode a hex string into base 36 formatted string.

For example, these are sample inputs and outputs.

ID and reversed B36 encoding

3028354D8202028000000000,CHL58FYDITHJ83VN0G1 3028354D8202028000000001,DHL58FYDITHJ83VN0G1 3028354D8202028000000002,EHL58FYDITHJ83VN0G1

Suggestions are highly appreciated.

BenH
  • 2,100
  • 2
  • 22
  • 33
  • 1
    What's the problem? Where are you struggling with the obvious approach? – Kerrek SB Dec 19 '11 at 15:27
  • As @KerrekSB alluded to, what have you tried? What has not worked with what you tried? What did you expect and what happened with your current attempt? – cdeszaq Dec 19 '11 at 15:28
  • Online conversion http://www.darkfader.net/toolbox/convert/ – Bala Srinivas Chikkala Dec 19 '11 at 15:29
  • I am not into embedded engineering, so not aware of these conversions in depth. Could find only public long decode(final String value) { return Long.parseLong(value, 36); } public String encode(final long value) { return Long.toString(value, 36); } But this for decimal types, and hexadecimal string are failing with these. – Bala Srinivas Chikkala Dec 19 '11 at 15:33

2 Answers2

9

Have you tried:

String convertHexToBase36(String hex) {
  BigInteger big = new BigInteger(hex, 16);
  return big.toString(36);
}
rossum
  • 15,344
  • 1
  • 24
  • 38
1

Thanks @rossum for your help and patience.

I could now do a conversion from hex to base36 and vice-versa as per my requirements.

public static String convertHexToBase36(String hex)
{
    BigInteger big = new BigInteger(hex, 16);
    StringBuilder sb = new StringBuilder(big.toString(36));
    return sb.reverse().toString();
}

public static String convertBase36ToHex(String b36)
{
    StringBuilder sb = new StringBuilder(b36);
    BigInteger base = new BigInteger(sb.reverse().toString(), 36);
    return base.toString(16);
}

I just did reverse B36 encoding. Loads of applause to @rossum for his patience and help.