15

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.

Ling Zhong
  • 1,744
  • 14
  • 24
Rasik Suhail
  • 206
  • 1
  • 2
  • 8

3 Answers3

35

The values starting 0x are hexadecimals. 0x0A is \n newline character and 0x0D is \r return character. You can read more about how to convert them here, or use the conversion chart

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

Briefly:

  • when the data is 0x0A, the newline character \n, it is skipped and not added to the arr_byte
  • when the data is 0x0D, the return character \r, it builds a buffer from arr_byte and send the buffer to the UI Activity
  • when the data is any other character, it is added to arr_byte

Hope this helps.

Ling Zhong
  • 1,744
  • 14
  • 24
  • @RasikSuhail glad to help :) It's always good to up the answer and mark it as solution if it solves the problems stated in your question. – Ling Zhong Oct 19 '15 at 07:29
0

The history of these two characters comes from the actions in a typewriter. To start a new line there are two mechanical actions: return to take the carriage back to the beginning of the current line, and new line to advance the paper to the new blank line below. So there are two codes in the ASCII for these two actions. Both were initially used together in earlier text editors in the same order (return, new line), but more modern editors often require only one, usually return.

0

My understanding is that the origin of carriage return and line feed characters was in 1901 with Murray's modifications to Baudot code (ITU1).

This enabled the standardization of teletypes & teleprinters.

The current names of the codes are related to the original meanings. Carriage return caused the print "carriage" to "return" to the left hand side. Line feed caused the paper feeding mechanism to advance one line.


In modern usage, the CR and LF codes have roughly the same meaning, though this now depends entirely on software. It is worth noting that different operating systems use CR and/or LF as "line separators" in simple text files. The conventions are OS-specific.

At some point the original name "line feed" transmogrified into "newline" in common usage. However, there is a distinct NL code in EBCDIC ... which is conventionally mapped to NEL (U+85) in Unicode.

References: Wikipedia

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216