【问题标题】:how to convert input stream to byte array to string in android如何在android中将输入流转换为字节数组到字符串
【发布时间】:2019-02-14 18:50:37
【问题描述】:

我正在研究 RFID 串行端口。在这个我有一个源代码,像这样new SerialPort(new File("/dev/ttyS3"), 19200);它的工作完美我得到了一个输出,但不是很好的编码我尝试了所有的编码,但它不起作用。

我从InputStream 得到一个输出:

mInputStream = mSerialPort.getInputStream();

private byte[] reciveBuffer=new byte[128];;
private int size = 0;

size = mInputStream.read(reciveBuffer);
 //i checked all encodings
String message = new String(reciveBuffer,0, size,Charset.forName("iso-8859-1"));
String message2 = new String(reciveBuffer,0, size,Charset.forName("windows-1252"));

assert message2.charAt(1) == '\u00E4';
String message3 = new String(reciveBuffer,0, size,Charset.forName("US-ASCII"));
String encode=((reciveBuffer[0] & 128) == 0) ? "UTF-8" : "windows-1252";
String display = new String(reciveBuffer, 0, size);

但我得到的是字符串:

3������`������`��xf���f���

来自所有编码。

【问题讨论】:

    标签: android arduino serial-port inputstream bytearrayoutputstream


    【解决方案1】:

    您可以尝试以下功能:

    private String Buff2String(byte[] buff) {
        String retVal = "";
    
        for(byte c : buff) {
            int i = c & 0xFF;
            retVal += (char)i;
        }
    
        return retVal;
    }
    

    【讨论】:

      【解决方案2】:

      您使用的是扩展 ascii (>127) 吗?

      【讨论】:

      • 我不知道我使用的是普通编码但有一个.so库串行端口
      猜你喜欢
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-08
      • 2015-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多