String.format equivalent in Java 1.4 - java

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?

You could use something like this snippet:
String hexString = Integer.toHexString(octet);
if (hexString.length() < 2) {
hexString = "0" + hexString;
}

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

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

Retrotranslator supports String.format translation to JDK 1.4

Related

Java string split gives different outputs on Windows and linux

Please see the below code --
String s11 ="!country=India ";
String[] ss =s11.split("((?<=[!&|])|(?=[!&|]))");
System.out.println(ss.length);
for(String s :ss) {
System.out.println(s);
}
On Windows it gives
2
!
country=India
Whereas with Ubuntu it gives
3
!
country=India
Why would that be ?
This behavior is not because of different operating systems, but likely different versions of the JVM are used.
This "behavior change" has caused bugs to be filed incorrectly for Java 8.
The documentation has been updated for JDK 8, and is also discussed at length in this question, where split in Java 8 removes empty strings at the start of the result array. This is why the additional empty string before the ! is not created (hence the length of 2 instead 3).
Notice the difference in documentation for the split() method in Java 7 and in Java 8 for the Pattern class, and the string class (Java 7, Java 8) respectively. See the original question linked for further information on this.
I have also reproduced this issue on Java 7: sun-jdk-1.7.0_10 (ideone) and Java 8 sun-jdk-8u25 (ideone). See the Java versions here. Java 8's split will provide not add the extra empty string into the array, while Java 7's split will.
This it is not because of the system being Linux or Windows, but rather the JVM version. You can double check your JVM's version with java -version

What is the equivalent of the C# Convert.ToInt32 in Java

I am trying to translate some C# code into java and would like to know what is the java equivalent of System.Convert.ToInt32(char):
Converts the value of the specified Unicode character to the
equivalent 32-bit signed integer.
Convert.ToInt32(letter);
"Convert.ToInt32(someChar)" does exactly what "(int)someChar" does.
Since "(int)someChar" is available in Java, why not use that?
When testing the various options, use '5' as a test - some options will convert this simply to the integer 5, but you will want the integer 53 to match the original C# behavior of Convert.ToInt32.

PHP equivalent of getBytes in Java

I have the below Java code:
String Str1 = new String("Welcome to Tutorialspoint.com");
System.out.println(Str1.toString().getBytes("UTF-8"));
The output on a 64-bit Windows 7 machine is: "[B#7041825e".
What would be the above code's PHP equivalent?
There isn't. PHP doesn't have a distinction between strings and a bunch of bytes.
You may use strlen() for that purpose.
In your case, mb_strlen() will do.
... for more reading: http://php.net/manual/en/function.mb-strlen.php

Byte alignment problem using Preon

Hello everybody :)
I am currently using preon for a spare time project, and I have encountered the following problem: I am trying to read a fixed length String with the following code:
#Bound int string_size;
#ByteAlign #BoundString(size = "string_size") my_string;
The file specification expects a variable padding, so that the next block's offset is a multiple of 4.
For example, if string_size = 5, then 3 null bytes will be added, and so on. I initially thought that the #ByteAlign annotation did exactly this, however, looking into the source code, I realized that it wasn't the case.
I tried to make this quick fix:
#If ("string_size % 4 == 2") #BoundList(size = "2", type = Byte.class) byte[] padding;
Sadly, Limbo doesn't seem to support the "%" operator. Is there a way around this?
(Also, where/how can I get the latest version?)
Thanks in advance.
Preon currently doesn't have a solution for your issue built-in. As you said, it's expression language doesn't have a modulo operator, and it looks like you could use one. You can however implement your own CodecDecorator, which is probably the thing you want to do. You could implement a CodecDecorator that inserts a Codec reading a couple of extrac bytes after it decoded the value.
The latest version of Preon is at Codehaus:
git://git.codehaus.org/preon.git
You could checkout the head, but there's also a separate branch called PREON-35 that has the bits for doing what is discussed over here.

Java 7 closure syntax

I download the last Java build b96- Feature Complete for testing the new JDK features
but I can't figure out which syntax using for testing closures!
Can I test it?
Which syntax has been approved in the final release?
I can't be certain, but I think this syntax:
// function expressions
#(int i, String s) {
System.println.out(s);
return i + s.length();
}
// function expressions
#(int i, String s) (i + s.length())
// function types
#int(int, String)
Is going to make it through as per http://docs.google.com/Doc?id=ddhp95vd_0f7mcns
To answer your question, no final syntax has been approved and, despite M8 being listed as the feature-complete milestone, it doesn't have all the proposed features. You can read here about the feature in its current form, but much discussion is going on now and it has quite a ways to go. Additionally, the syntax is going to be revisited and likely changed (at least some) later, once more pressing issues are worked out.
Also, project-lambda code is being worked on in a fork of the main line JDK7 (I believe), so I don't think any of it would be in the build you downloaded.

Categories