Java 0x0A 和 0x0D 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33208518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:26:57  来源:igfitidea点击:

Difference between 0x0A and 0x0D

javabluetoothhexcharacterbuffer

提问by Rasik Suhail

I was studying about bluetooth and I was trying to write the code to keep listening to the input stream while connected and i came across this following code snippet:

我正在研究蓝牙,我试图编写代码以在连接时继续收听输入流,我遇到了以下代码片段:

int data = mmInStream.read();
   if(data == 0x0A) { 
                } else if(data == 0x0D) {
                    buffer = new byte[arr_byte.size()];
                    for(int i = 0 ; i < arr_byte.size() ; i++) {
                        buffer[i] = arr_byte.get(i).byteValue();
                    }
                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(BluetoothState.MESSAGE_READ
                            , buffer.length, -1, buffer).sendToTarget();
                    arr_byte = new ArrayList<Integer>();
                } else {
                    arr_byte.add(data);
                }

Can someone explain what is the difference between 0x0A and 0x0D. And also give a brief explanation about this code. Kindly share your views.

有人可以解释 0x0A 和 0x0D 之间的区别是什么。并对这段代码做一个简单的解释。请分享您的观点。

采纳答案by Ling Zhong

The values starting 0xare hexadecimals. 0x0Ais \nnewline character and 0x0Dis \rreturn character. You can read more about how to convert them here, or use the conversion chart

开始的值0x是十六进制。0x0A\n换行符,0x0D\r返回符。您可以在此处阅读有关如何转换它们的更多信息,或使用转换表

The code essentially runs different blocks of logic depending on what value of datais read from the mmInStream

该代码基本上运行不同的逻辑块,具体取决于datammInStream

Briefly:

简要地:

  • when the datais 0x0A, the newline character \n, it is skipped and not added to the arr_byte
  • when the datais 0x0D, the return character \r, it builds a buffer from arr_byteand send the buffer to the UI Activity
  • when the datais any other character, it is added to arr_byte
  • datais 是0x0A换行符时\n,它会被跳过并且不会添加到arr_byte
  • datais0x0D返回字符时\r,它会从中构建缓冲区arr_byte并将缓冲区发送到 UI 活动
  • data是任何其他字符时,它被添加到arr_byte

Hope this helps.

希望这可以帮助。