7

Currently i have a method calling String.format() in Java 5 and it's working perfectly

String.format("%02x", octet) //octet is a int type

However due to some issue we need to deploy this code in a JDK 1.4 environment, and String.format doesn't exists in 1.4.

Anyone knows any alternative way to perform this function?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
ipohfly
  • 1,959
  • 6
  • 30
  • 57
  • 4
    Support (*and security updates*) for Java 1.4 [*ended in October 2008*](https://en.wikipedia.org/wiki/Java_version_history#J2SE_1.4_.28February_6.2C_2002.29) (that's 4 years ago!). – Joachim Sauer Nov 02 '12 at 11:53
  • Not sure if nostalgic, or just crazy. You should really try to get rid of that requirement. Is the machine too old? Is there no will for change? Is it too much of a hassle to check if other programs are Java 6 or 7 compatible? :( – Scorpio Nov 02 '12 at 11:58
  • 1
    Support (and security updates) for Java 5.0 ended in October 2009 and for Java 6 it will end Feb 2013. – Peter Lawrey Nov 02 '12 at 12:02
  • 1.4?!?!? wow, that's some old sh@$. – rees Nov 02 '12 at 12:03
  • Ya we notice that the SDK is too old as well, but alas the decision making power is not in our hand :) There're already a scheduled plan to upgrade the system but not sure when it will take place. – ipohfly Nov 02 '12 at 12:03
  • @ipohfly: I'm well aware that the decisions is not usually in the hands of the developers, but lack of support and security updates are generally some of the more effective arguments to bring before management ("we have no one to blame when we get hacked now!"). – Joachim Sauer Nov 02 '12 at 12:04
  • yeah, trust me, the issue had been on the plate for quite some time. There's a more concrete timeline with the transition plan now, let's hope it won't get thrown into the rubbish bin =p – ipohfly Nov 02 '12 at 13:53

4 Answers4

3

You could use something like this snippet:

String hexString = Integer.toHexString(octet);
if (hexString.length() < 2) {
    hexString = "0" + hexString;
}
Keppil
  • 45,603
  • 8
  • 97
  • 119
  • 1
    The `while` can be replaced with an `if` since there will be at most one `0` missing. – Joachim Sauer Nov 02 '12 at 12:00
  • 1
    or use apache commons to pad the string, it's java 1.2+ http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringUtils.html#leftPad(java.lang.String, int, char) – Denis Tulskiy Nov 02 '12 at 12:19
1

You need to use Integer.toHexString(int) and pad the text yourself.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 2
    hmm should i use `Integer.toHexString(int)` instead of that since i'm looking for hex string? – ipohfly Nov 02 '12 at 11:52
0

I think you should take a look at Retroweaver which lets you deploy Java 1.5 on a 1.4 JVM.

maba
  • 47,113
  • 10
  • 108
  • 118
0

Retrotranslator supports String.format translation to JDK 1.4

ilalex
  • 3,018
  • 2
  • 24
  • 37
  • already java is providing why need to go for a library for a simple change – Dungeon Hunter Nov 02 '12 at 12:44
  • JDK 1.4 is not providing String.format method see javadocs for more info. Retrotranslator is not just a library. It is a byte-code manipulation tool. – ilalex Nov 02 '12 at 13:24