Byte in the hex conversion of Bluetooth communication when Android

When Android development, input or received data are generally of type String, expressed in hexadecimal; the hardware device is received byte stream type byte. So we need to do in the development of the two conversion data.

byte is a data type, an integer, is only 8 bits. There are four integers in Java: byte, short, int, long. Difference is the numerical ranges, byte is 8 bits, the range is -127 to 128; the other types such as int, is 32 bits; Short is 16; Long is 64 bits.

The hex, hexadecimal, not a data type.

In Java byte binary representation occupies eight, and we know that each character hexadecimal need to use 4-bit binary bits to represent. So we can convert each byte into the corresponding two hexadecimal characters, i.e., the upper 4 bits of the byte and the lower 4 bits are converted into the corresponding hexadecimal characters, and combined to obtain a 16 byte hexadecimal converter the results of the string.

Similarly, the opposite conversion is to convert the two hex characters into a byte, the principle above.

Based on the above principles, we can be byte [] array into a hexadecimal string, of course, can also be converted to a hexadecimal string byte [] array up.

In fact, is simply the binary to hex conversion, given that we often represented as decimal data byte of -127 to 128, when we think can be seen as a binary decimal turn sub-hex, but computer in hexadecimal or binary transfer.

https://www.cnblogs.com/Free-Thinker/p/6529584.html

Other details and please just turn major step in this blog, the authors write very detailed and very thorough.

Guess you like

Origin blog.csdn.net/Smile_Qian/article/details/82025304