Я использовал приведенный ниже код, чтобы Преобразовал мои двоичные данные в обычный текст, используя кодировку Latin9, и попытался определить кодировку, выполненную в выходном простом тексте. Мой код Java всегда обнаруживал кодировку UTF8.
Jar ICU4j для определения кодировки: ссылка для скачивания
Ввод: двоичные данные
Вывод: строка ( закодировано латиницей9)
Код: Выделить всё
import java.nio.charset.Charset;
import com.ibm.icu.text.CharsetDetector;
import com.ibm.icu.text.CharsetMatch;
public class ActBinaryDataToText_Latin9 {
/**
*
* @param inParamBinaryData Binary content to convert to text.
* @param result.outParamText Result of conversion from BinaryData to Text.
**/
// output parameters
public String outParamText;
public static void main(String[] args) throws Exception {
String binaryData = "00101010";
//Method 1: to convert binary data to plain text
String cleanBinaryData = binaryData.replaceAll(" ", "");
// Determine the length of the clean binary data
int length = cleanBinaryData.length();
// Create a byte array to hold the binary data
byte[] bytes = new byte[length / 8];
// Convert each 8-bit substring to a byte value
for (int i = 0; i < length; i += 8) {
String byteString = cleanBinaryData.substring(i, i + 8);
bytes[i / 8] = (byte) Integer.parseInt(byteString, 2);
}
String latin9Text1 = new String(bytes, Charset.forName("ISO-8859-15"));
//Method 2: to convert binary data to plain text
byte[] binaryBytes = binaryToBytes(binaryData);
// Convert bytes to Latin-9 encoded text
String latin9Text2 = new String(binaryBytes, Charset.forName("ISO-8859-15"));
//Detecting the encoding of both the Latin9text: expected(ISO-8859-15)
DetectEncoding(latin9Text1);
DetectEncoding(latin9Text2);
}
public static byte[] binaryToBytes(String binaryData) {
int length = binaryData.length() / 8; // Calculate the length of the byte array
byte[] bytes = new byte[length];
for (int i = 0; i < length; i++) {
// Convert each 8-bit substring of binaryData into a byte
String byteString = binaryData.substring(i * 8, (i + 1) * 8);
bytes[i] = (byte) Integer.parseInt(byteString, 2);
}
return bytes;
}
public static void DetectEncoding(String Latin9Text) {
CharsetDetector detector = new CharsetDetector();
byte[] inputBytes = Latin9Text.getBytes();
// Set the input text for detection
detector.setText(inputBytes);
// Detect the encoding
CharsetMatch match = detector.detect();
// Get the detected encoding
String detectedEncoding = match.getName();
// Print the detected encoding
System.out.println("Detected encoding: " + detectedEncoding);
}
}
Обнаруженная кодировка: UTF-8
Обнаруженная кодировка: UTF-8
Подробнее здесь: https://stackoverflow.com/questions/784 ... 9-encoding
Мобильная версия