Remy Strijker
Remy Strijker

Reputation: 261

Need Java equivalent to pack("H32", param) from php

I need a java equilavent from this php code:

$string = "testing";
$hexchal = pack('H32', $string);

I did search the internet, but found no answer.

Thanks in advance!

Upvotes: 0

Views: 1017

Answers (1)

jarchuleta
jarchuleta

Reputation: 1241

I don't think your code above works. You expect the string to be in Hex format. This should do the trick, but not as nice as php.

String hex = "4a616d6573";
StringBuilder output = new StringBuilder();
for (int i = 0; i < hex.length(); i+=2) {
    String str = hex.substring(i, i+2);
    output.append((char)Integer.parseInt(str, 16));
}
System.out.println(output);

Upvotes: 2

Related Questions