when i encode a pdf file into base64 binary(using commons lib) in java , I got string(sample) like "SGVsbG8gV_29y-bGQ="
But if i encode the same file using perl , I got like "SGVsbG8gV/29y+bGQ="
Difference in string: / instead of _ , + instead of -
Why i am getting like this? Or, Is there any way to fix without string replace ?
in java
byte[] data;
try (java.io.FileInputStream fin = new java.io.FileInputStream(new java.io.File("file.pdf"))) {
data = new byte[fin.available()];
fin.read(data);
}
return data;
System.out.println("ecncoded value is " + DatatypeConverter.printBase64Binary(data));
In Perl
use MIME::Base64;
open (PDF, "file.pdf") or die "$!";
$raw_string = do{ local $/ = undef; <PDF>; };
$encoded = encode_base64( $raw_string );
print " \n";
print " $encoded ";
// Java

// Perl

The Java code is giving you URL-safe base64, i.e the output can be used in URLs. The Perl version is not. You can blindly replace the characters with the other values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With