15

The question has been answered for integers printed in decimal format, but I'm looking for an elegant way to do the same with integers in non-decimal format (like binary, octal, hex).

Creation of such Strings is easy:

String intAsString = Integer.toString(12345, 8);

would create a String with the octal represenation of the integer value 12345. But how to format it so that the String has like 10 digits, apart from calculating the number of zeros needed and assembling a new String 'by hand'.

A typical use case would be creating binary numbers with a fixed number of bits (like 16, 32, ...) where one would like to have all digits including leading zeros.

Community
  • 1
  • 1
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • 2
    What you mean for "elegant" solution is a library function. I think that there is no more elegant solution than calculating the zeros needed and pre-concatenate them. Anyway, if there were a Math trick/algorithm to do what you want to do, it would be different for binary and octal and hex... – pakore Jun 30 '10 at 13:43
  • @pakore - yes, you're right, some 'library' function already present in the Java API would be 'most elegant' for me ;) - I just hate reimplementing this padding code again and again. – Andreas Dolk Jun 30 '10 at 13:54
  • Well, have a look at the NumberFormat class, create a OctalFormat, HexFormat and BinaryFormat class with the methods to pad zeros, create a feature proposal, submit your code, wait a couple of months, upgrade your libraries and then you will have it as a library function :). Or you can create your own library helper for your project :P – pakore Jun 30 '10 at 14:28
  • Don't reimplement it again and again then. Use an existing library that provides it, even if it isn't the standard Java library. This is basically a string padding problem, not so much a number problem. – ColinD Jun 30 '10 at 14:43
  • @ColinD - Guava is my favorite answer right now and if I don't get a nice answer/pattern to solve it with standard Java API, then you get the 'accept' ;) – Andreas Dolk Jun 30 '10 at 14:55

5 Answers5

25

For oct and hex, it's as easy as String.format:

assert String.format("%03x", 16) == "010";
assert String.format("%03o", 8) == "010";
gustafc
  • 28,465
  • 7
  • 73
  • 99
  • yes..but..I was looking for a general approach, working with any given radix (with a practical upper limit because we need letters to represent digits, just like in hex values) – Andreas Dolk Jun 30 '10 at 14:53
15

With Guava you could just write:

String intAsString = Strings.padStart(Integer.toString(12345, 8), 10, '0');
ColinD
  • 108,630
  • 30
  • 201
  • 202
6

How about this (standard Java):

private final static String ZEROES = "0000000000";

// ...

String s = Integer.toString(12345, 8);
String intAsString = s.length() <= 10 ? ZEROES.substring(s.length()) + s : s;
Jesper
  • 202,709
  • 46
  • 318
  • 350
  • Thanks a lot! That's a pattern that's easy enough to remember! – Andreas Dolk Jun 30 '10 at 15:47
  • 6
    Ew. This does not clearly state what it's doing, and it's not flexible at all. If you want to avoid a library just write your own string padding method and be done with it. I sure wouldn't want to remember a "pattern" like this and I absolutely wouldn't want to see it used multiple places in code. – ColinD Jun 30 '10 at 15:52
  • @ColinD - I partially agree. If I need padding in an application context then I'll probably include guava. But for all those small snippets and trials, Jespers solution is what *I* was looking for. – Andreas Dolk Jun 30 '10 at 16:49
  • I didn't show it in my answer, but I'd put it in a method, maybe in a `StringUtils` class (so that the `ZEROES` also has a place to live) instead of repeating it in multiple places in the code. – Jesper Jul 01 '10 at 06:56
  • You shouldn't concatenate strings – Dragas Apr 19 '19 at 19:07
3

Printing out a HEX number, for example, with ZERO padding:

System.out.println(String.format("%08x", 1234));

Will give the following output, with the padding included:

000004d2

Replacing x with OCTAL's associated formatting character will do the same, probably.

Armandt
  • 311
  • 1
  • 3
  • 8
2

Here's a more reuseable alternative with help of StringBuilder.

public static String padZero(int number, int radix, int length) {
    String string = Integer.toString(number, radix);
    StringBuilder builder = new StringBuilder().append(String.format("%0" + length + "d", 0));
    return builder.replace(length - string.length(), length, string).toString();
}

The Guava example as posted by ColinD is by the way pretty slick.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555