Skip to main content
added 196 characters in body
Source Link
jsotola
  • 1.5k
  • 2
  • 12
  • 20

You receive the data as int, but you store it as byte.

For example, when you call the serial library function to receive a data byte, the library will return either the data or a -1 if a problem has occurred.

The binary value of -1 is a byte 11111111 or int 1111111111111111.

The binary value of 255 is a byte 11111111 or int 0000000011111111.

I think that you can see the problem if you use bytebyte data and the data is 255.

So, if you receive an int -1, then you do not process the data. Otherwise you cast the int to uint8_t.

You could receive byte data, but then the value of 255 would be undefined.

You receive the data as int, but you store it as byte.

For example, when you call the serial library function to receive a data byte, the library will return either the data or a -1 if a problem has occurred.

The binary value of -1 is a byte 11111111 or int 1111111111111111.

The binary value of 255 is a byte 11111111 or int 0000000011111111.

I think that you can see the problem if you use byte data and the data is 255.

You receive the data as int, but you store it as byte.

For example, when you call the serial library function to receive a data byte, the library will return either the data or a -1 if a problem has occurred.

The binary value of -1 is a byte 11111111 or int 1111111111111111.

The binary value of 255 is a byte 11111111 or int 0000000011111111.

I think that you can see the problem if you use byte data and the data is 255.

So, if you receive an int -1, then you do not process the data. Otherwise you cast the int to uint8_t.

You could receive byte data, but then the value of 255 would be undefined.

Source Link
jsotola
  • 1.5k
  • 2
  • 12
  • 20

You receive the data as int, but you store it as byte.

For example, when you call the serial library function to receive a data byte, the library will return either the data or a -1 if a problem has occurred.

The binary value of -1 is a byte 11111111 or int 1111111111111111.

The binary value of 255 is a byte 11111111 or int 0000000011111111.

I think that you can see the problem if you use byte data and the data is 255.