Получение странных результатов из кодовых точек строки Java на компьютере с WindowsJAVA

Программисты JAVA общаются здесь
Anonymous
Получение странных результатов из кодовых точек строки Java на компьютере с Windows

Сообщение Anonymous »

Код: Выделить всё

import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

public class Zapper {
private static final String HTML_DELIMITER = "&#";
private String input;

public Zapper() {
}

public String unicodePrint(String input) {
if (input != null && !input.isEmpty()) {
StringBuilder sbTitle = new StringBuilder();
System.err.println(input); // 挑战
final int length = input.length();
for (int offset = 0; offset < length;) {
final int codepoint = input.codePointAt(offset);
if (codepoint > 127) {
sbTitle.append(HTML_DELIMITER + codepoint);
} else {
sbTitle.appendCodePoint(codepoint);
}
offset += Character.charCount(codepoint);
}
input = sbTitle.toString();
System.err.println(input); // &#230&#338&#8216&#230&#710&#732
}
return input;
}

public static void main(String[] args) {
Zapper z = new Zapper();
z.input = "挑战";
if(args.length > 0) {
System.out.println("Restarting:%s :%d :%d".formatted(z.input,z.input.codePointAt(0),z.input.codePointAt(1)));
}
String encodeString = Base64.getEncoder().encodeToString(z.input.getBytes());
byte[] decodeBytes = Base64.getDecoder().decode(encodeString);
String decodeStringD = new String(decodeBytes,Charset.forName("ISO-8859-1"));
String decodeStringW = new String(decodeBytes,Charset.forName("windows-1252"));
String decodeStringU = new String(decodeBytes,Charset.forName("UTF-8"));
String decodeString = new String(decodeBytes);
z.unicodePrint(decodeStringD);
z.unicodePrint(decodeStringW);
z.unicodePrint(decodeStringU);
z.unicodePrint(decodeString);
if(args.length < 1) {
restartApplication();
}
}
private static Process restartApplication() {
Process process = null;
while (process == null) {
List command = new ArrayList();
command.add("java");
command.add("-Ddebugging=false");
command.add("-DdebugUnit=None");
command.add("-jar");
command.add("zapper-1-jar-with-dependencies.jar");
command.add("restarted");
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectOutput(Redirect.INHERIT);
pb.redirectError(Redirect.INHERIT);
try {
process = pb.start();
Thread.sleep(5_000);
} catch (IOException | InterruptedException e) {
System.out.println(e.getMessage());
}
}
return process;
}

}
Я привел пример, чтобы облегчить расследование. Примерно так я и делаю, но можно пойти и тем, что есть. В любом случае, очевидно, каков ответ на оригинальный вопрос. Но СЕЙЧАС перезапуск просто приводит к плохим последствиям, и я не знаю, почему. Кажется, один из декодеров должен сработать, лол.
результаты:
ææ
&#230&#140&#145&#230&#136&#152
挒战
&#230&#338&#8216&#230&#710&#732
挑战
&#25361&#25112
挑战
&#25361&#25112
Перезапуск:?? :25361 :25112
??
??
??
??
??
??
??
??

Вернуться в «JAVA»