Reputation: 3299
I'm converting the following Perl script to Java. Please explain what it does and the best approach via Java.
#!/usr/bin/perl -w
use MIME::Base64;
use Convert::BinHex;
print encode_base64(pack "H*", "06d8f33b9c3dd94f8f45a5ef0bd54c63f0cd3113b7b5ebae79807041f7e2f8975352367266a926ea8a2a93ca");
Upvotes: 0
Views: 488
Reputation: 97825
Even though I don't know Perl I'd make an informed guess that it takes binary data in hexadecimal form (where each byte is represented by two characters in the range [0-9a-f]) and converts it to a base 64 string.
In Java, you use DatatypeConverter::parseHexBinary
and DatatypeConverter::printBase64Binary
.
String hexData = "06d8f33b9c3dd94f8f45a5ef0bd54c63f0cd3113b7b5ebae79807041f7e2f8975352367266a926ea8a2a93ca";
String base64data = DatatypeConverter.printBase64Binary(
DatatypeConverter.parseHexBinary(hexData));
System.out.println(base64data);
Upvotes: 6
Reputation: 6433
import org.apache.commons.codec.binary.Base64;
Base64.encodeBase64(string you want to encode.getBytes());
Upvotes: 0