String.format equivalent in Java 1.4
Asked Answered
S

4

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?

Steffy answered 2/11, 2012 at 11:45 Comment(7)
Support (and security updates) for Java 1.4 ended in October 2008 (that's 4 years ago!).Behn
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? :(Frig
Support (and security updates) for Java 5.0 ended in October 2009 and for Java 6 it will end Feb 2013.Lariat
1.4?!?!? wow, that's some old sh@$.Biagio
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.Steffy
@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!").Behn
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 =pSteffy
R
3

You could use something like this snippet:

String hexString = Integer.toHexString(octet);
if (hexString.length() < 2) {
    hexString = "0" + hexString;
}
Redouble answered 2/11, 2012 at 11:53 Comment(2)
The while can be replaced with an if since there will be at most one 0 missing.Behn
or use apache commons to pad the string, it's java 1.2+ commons.apache.org/lang/api-2.5/org/apache/commons/lang/…, int, char)Autoionization
L
1

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

Lariat answered 2/11, 2012 at 11:50 Comment(1)
hmm should i use Integer.toHexString(int) instead of that since i'm looking for hex string?Steffy
P
0

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

Pollinosis answered 2/11, 2012 at 12:38 Comment(0)
C
0

Retrotranslator supports String.format translation to JDK 1.4

Curiosa answered 2/11, 2012 at 12:42 Comment(2)
already java is providing why need to go for a library for a simple changeClow
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.Curiosa

© 2022 - 2024 — McMap. All rights reserved.