【发布时间】:2013-01-09 15:17:16
【问题描述】:
我正在使用终端仿真器库来创建终端,然后使用它将通过串行输入的数据发送到串行设备。图书馆可以看到here。
当我将数据输入终端时,正在发送/接收一系列奇怪的字符。我认为 unicode 替换字符是通过串行发送的,串行设备不知道它是什么并返回 ~0。
当我写“测试”时出现在终端中的屏幕截图:
还有显示发送的字符串和接收的数据的日志。
我创建了一个 EmulatorView,它是终端视图。它提到了钻石here。
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);
}
}
有没有办法解决这个问题?任何人都可以在库类中看到为什么会发生这种情况吗?,如果我愿意,我如何在 java 中引用 � 来解析它?我不能说如果 (!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;
}
这就是我所说的发送数据。 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();
}
}
一旦发送数据,就会在此处收到回复:
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);
}
写入字符的库类的相关部分:
类的相关部分:
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();
}
【问题讨论】:
-
只是出于好奇,如果你输入 hello 这个词,你会得到 4 shevrons
-
在我输入每个字符后我得到 ~0~0~0 所以你好是:h~0~0~0e~0~0~0l~0~0~0l~0~0~0o ~0~0~0
-
我可能会抓住稻草,但我问的原因是因为查看您的 sendText 方法,我只是想考虑一下您说您使用了其他编码,这是否可能是一个简单的编码错误。如果您删除循环中的 If 语句并将 c 传递给 mapAndSend,会发生什么?
-
我会试试看,代码来自库,所以我没有碰过它。谢谢!
-
@Paul 打印出您需要执行以下操作的字节:
String log = ""; for( byte i : byteArray ) { log += String.format("0x%02X, ", i); } Log.d(TAG, log);
标签: java android unicode character-encoding