I am receiving data from a hardware device. I receive it as an array of bytes. I want to build and store a String that can show me the data of the array of bytes. This is what I have:
byte[] buffer = new byte[BUFFER_SIZE];
bytes = mmInStream.read(buffer);
String[] str = new String[bytes];
StringBuffer readMessage = new StringBuffer();
for(int i=0; i<bytes; ++i){
str[i] = Integer.toHexString(buffer[i]);
while (str[i].length() < 8){ //standarize size
str[i] = '0' + str[i];
}
readMessage.append(str[i]);
}
The main problem that I have is that I am receiving unexpected bytes when I transform the bytes to String. I am receiving pure bytes so I am expecting the values to range from 0x00 to 0xFF. However, some of the bytes are transformed to bytes like 0xFFFFFF09. Why is this happening??
mmInStream.read(buffer)returns-1?new String(buffer, 0, bytes)constructor](docs.oracle.com/javase/6//docs/api/java/lang/…, int, int))?