Pau*_*aul 10 java unicode android character-encoding
我正在使用终端仿真器库来创建终端,然后我用它将串行输入的数据发送到串行设备.图书馆可以在这里看到.
当我向终端输入数据时,正在发送/接收一系列奇怪的字符.我认为unicode替换字符是通过串行发送的,串行设备不知道它是什么并返回〜0.
我写"test"时终端中显示的内容的屏幕截图: 
并显示发送的字符串和收到的数据的日志. 
我创建了一个EmulatorView,它是终端视图.它在这里提到钻石.
private void sendText(CharSequence text) {
int n = text.length();
char c;
try {
for(int i = 0; i < n; i++) {
c = text.charAt(i);
if (Character.isHighSurrogate(c)) {
int codePoint;
if (++i < n) {
codePoint = Character.toCodePoint(c, text.charAt(i));
} else {
// Unicode Replacement Glyph, aka white question mark in black diamond.
codePoint = '\ufffd';
}
mapAndSend(codePoint);
} else {
mapAndSend(c);
}
}
} catch (IOException e) {
Log.e(TAG, "error writing ", e);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有什么办法解决这一问题?任何人都可以在库类中看到为什么会发生这种情况吗?如果我想的话,如何在java中引用to甚至解析它?我不能说是否(!str.contains(" ")我接受它.
当我输入终端时,运行:
public void write(byte[] bytes, int offset, int count) {
String str;
try {
str = new String(bytes, "UTF-8");
Log.d(TAG, "data received in write: " +str );
GraphicsTerminalActivity.sendOverSerial(str.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
Log.d(TAG, "exception" );
e.printStackTrace();
}
// appendToEmulator(bytes, 0, bytes.length);
return;
}
Run Code Online (Sandbox Code Playgroud)
这就是我所说的发送数据.sendData(Byte [] data)是一个库方法.
public static void sendOverSerial(byte[] data) {
String str;
try {
str = new String(data,"UTF-8");
if(mSelectedAdapter !=null && data !=null){
Log.d(TAG, "send over serial string==== " + str);
mSelectedAdapter.sendData(str.getBytes("UTF-8"));
}
} catch (UnsupportedEncodingException e) {
Log.d(TAG, "exception");
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
发送数据后,会在此处收到回复:
public void onDataReceived(int id, byte[] data) {
try {
dataReceived = new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.d(TAG, "exception");
e.printStackTrace();
}
try {
dataReceivedByte = dataReceived.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
Log.d(TAG, "exception");
e.printStackTrace();
}
statusBool = true;
Log.d(TAG, "in data received " + dataReceived);
((MyBAIsWrapper) bis).renew(data);
runOnUiThread(new Runnable(){
@Override
public void run() {
mSession.appendToEmulator(dataReceivedByte, 0, dataReceivedByte.length);
}});
viewHandler.post(updateView);
}
Run Code Online (Sandbox Code Playgroud)
库类的相关部分,其中写有字符:
课程相关部分:
private void sendText(CharSequence text) {
int n = text.length();
char c;
try {
for(int i = 0; i < n; i++) {
c = text.charAt(i);
if (Character.isHighSurrogate(c)) {
int codePoint;
if (++i < n) {
codePoint = Character.toCodePoint(c, text.charAt(i));
} else {
// Unicode Replacement Glyph, aka white question mark in black diamond.
codePoint = '\ufffd';
}
mapAndSend(codePoint);
} else {
mapAndSend(c);
}
}
} catch (IOException e) {
Log.e(TAG, "error writing ", e);
}
}
private void mapAndSend(int c) throws IOException {
int result = mKeyListener.mapControlChar(c);
if (result < TermKeyListener.KEYCODE_OFFSET) {
mTermSession.write(result);
} else {
mKeyListener.handleKeyCode(result - TermKeyListener.KEYCODE_OFFSET, getKeypadApplicationMode());
}
clearSpecialKeyStatus();
}
Run Code Online (Sandbox Code Playgroud)
我通过编辑我正在使用的库解决了这个问题。他们使用了一种将字节转换为 int 的方法,它接受一个 codePoint 并将其转换。因此,每次按键都会使用 4 个字节。我对此进行了更改,以便使用字节而不是整数。不再有额外的字节。与编码格式无关。