quoted-printableエンコーディングのデコードをするためのJavaプログラムを探しています。
以下はquoted-printableエンコードをデコードするための簡単なJavaプログラムです。
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
public class QuotedPrintableDecoder {
public static void main(String[] args) {
String encodedString = “Hello=20World=21”;
String decodedString = decodeQuotedPrintable(encodedString);
System.out.println(decodedString);
}
public static String decodeQuotedPrintable(String encodedString) {
try {
byte[] encodedBytes = encodedString.getBytes(StandardCharsets.US_ASCII);
StringBuilder decodedString = new StringBuilder();
int i = 0;
while (i < encodedBytes.length) {
if (encodedBytes[i] == ‘=’) {
int hex1 = Character.digit(encodedBytes[i + 1], 16);
int hex2 = Character.digit(encodedBytes[i + 2], 16);
if (hex1 != -1 && hex2 != -1) {
byte decodedByte = (byte) ((hex1 << 4) + hex2);
decodedString.append((char) decodedByte);
i += 3;
} else {
decodedString.append(‘=’);
i++;
}
} else {
decodedString.append((char) encodedBytes[i]);
i++;
}
}
return decodedString.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
} }
上記のコードでは、decodeQuotedPrintableメソッドを定義しています。このメソッドは、quoted-printable形式の文字列を受け取り、デコードされた文字列を返します。このメソッドでは、まず入力文字列をバイト配列に変換し、バイトごとにデコードして解読された文字をStringBuilderオブジェクトに追加しています。=HEX形式のエンコーディングに遭遇した場合は、それを対応するバイトに変換し、そのバイトをデコード文字列に追加します。最後に、StringBuilderオブジェクトを文字列に変換してデコード結果を返します。mainメソッドでは、encodedStringの例が用意され、decodeQuotedPrintableメソッドを呼び出してデコードします。デコード結果はコンソールに出力されます。