3

What is the best way to convert a byte array to a string with a radix of choice? There is a ton of examples here on S.O. and elsewhere to convert to a hex string. What I am mainly interested in here is converting to something other than a hex or decimal string; also a more generic way.

This is what I currently do:

byte[] input;
String MyStr = new BigInteger(input).toString(radix);

This works, but since Java has a concept of radix, as used in the Integer. This seems to be the explicit purpose as defined in Character. Shouldn't there be a better way of doing this rather than to first convert the byte array to a BigInteger? It feels like my Java knowledge misses some essential standard class?

EDIT: I would like to use this for a compressed way of representing (and printing) raw binary data of different types. This is the actual radix I currently use:

String MyStr = new BigInteger(data).toString(Character.MAX_RADIX);

The MAX_RADIX (36) uses a combination of lower case letters and numbers. This gives a decent compression, but would be even better if the radix could include UPPER letters, which is why I thought I may be missing something.

7
  • Can you give examples of the bytes and what you expect as the result? Commented Feb 24, 2015 at 22:11
  • 1
    It looks like a very object oriented way to me, you get the value representation which is then translated to string. Are you worried about performance? The code at least is self explanatory. Commented Feb 24, 2015 at 22:14
  • @Bohemian. Made a slight edit to include my usage radix. Commented Feb 24, 2015 at 22:29
  • I'm assuming your input is a byte[], not a Byte[], since there's no BigInteger constructor that takes a Byte[]. Commented Feb 24, 2015 at 22:43
  • @ericbn. Right! I changed it to byte[]. Commented Feb 24, 2015 at 22:45

2 Answers 2

3

It seems like you're looking for Base64 encoding. This uses 64 different characters to encode a value: upper case and lower case letters (that's 52 characters already), digits (10 more, including the '0'), and the '+' and '/' symbols (or '-' and '_' in the url-safe variation).

If you're using Java 8, there's the Base64 class:

String str = Base64.Encoder.encodeToString(data);

Otherwise, Apache Commons has a Base64 class too:

String str = Base64.encodeBase64String(data);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This is useful, and great its now part of Java8. I should have thought of that. I still sometimes need the radix to be 2 and 16 for debugging, but for the most part, Base64 should be ok.
0

That's a good one, actually, I'm not sure neither if there's a class to achieve that. But another way to do what you want without using BigInteger is to iterate over the array and get the output, something like:

for(byte b : input){
    System.out.print(Integer.toString(b & 0xFF, radix));
}

Not sure about an specific class doing a conversion, but this is another approach I know, maybe could be useful to you or to somebody else.

Happy coding.

Regards.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.