Arduino receiving negative bytes over serial.

I have been trying to send negative bytes over serial from processing to arduino. it seems that processing (or javas) bytes go from -128 to +127 whereas arduinos bytes are from 0 - 255 so while i am unable to send a byte of value greater that 127 on the processing end the arduino will convert all negative values into positives. is there a simple way that i can get around this? thanks.

The char type is typically a signed 8 bit integer (so -128 - 127). But I only like to use the char type for character data since the name doesn't make much sense for numerical data. For that, I use the equivalent uint8_t type.

gonadgranny:
I have been trying to send negative bytes over serial from processing to arduino. it seems that processing (or javas) bytes go from -128 to +127 whereas arduinos bytes are from 0 - 255 so while i am unable to send a byte of value greater that 127 on the processing end the arduino will convert all negative values into positives. is there a simple way that i can get around this? thanks.

Yes, just cast the values to byte in Java, they will have the correct representation even if they appear
negative to Java, the right bit pattern is in there.

The char type is typically a signed 8 bit integer (so -128 - 127). But I only like to use the char type for character data since the name doesn't make much sense for numerical data. For that, I use the equivalent uint8_t type.

thanks for this the uint8_t data type seems to work although it is still a positive value so i suppose to convert back to negative I would deduct the value from 255, is this the best way?

Yes, just cast the values to byte in Java, they will have the correct representation even if they appear
negative to Java, the right bit pattern is in there.

thanks i will give it a go.

pert:
The char type is typically a signed 8 bit integer (so -128 - 127). But I only like to use the char type for character data since the name doesn't make much sense for numerical data. For that, I use the equivalent uint8_t type.

char - Arduino Reference

int8_t?

Oops, you got it!

..i think i'll rephrase my question, what is the best way to convert back from the now positive int8_t to the negative value so i can use it in computations? thanks.

gonadgranny:
..i think i'll rephrase my question, what is the best way to convert back from the now positive int8_t to the negative value so i can use it in computations? thanks.

You clearly need to understand what is going on.

Java is casting a positive integer in the range (0..255) to a byte variable, which involves
doing nothing at all to the bits - the same 8 bits are in the byte variable.

Assuming the serial link sends those 8 bits verbatim, the Arduino receives the same 8 bits
and puts them into your byte variable at the Arduino end.

What those bits represent depends on context, what they are does not.

By the way in Java you can write byte b = (byte) 200; and you get
a compiler warning because 200 cannot be represented as a byte, but the variable still
gets set to the pattern 11001000 (which is explicitly required by the language specification)

If Processing is sending a value in the range -128 to 127, and you store the value you read from the serial port on the Arduino into a variable that can hold a value in that range, such as int8_t, then you do not have to do anything to have the same value on both ends.

If you insist on storing the value in a variable of a different type, well, good luck trying to guess which values should be subtracted from 255 and which shouldn't.

ok i have just tried to do some computations using the negative uint8_t which i sent over and its working now. got a bit confused because it still wasnt printing out as a negative number. thanks for all the help.

the only way i have been able to successfully use the value as a negative is by creating an int

Perhaps that is because you are NOT using an int8_t variable to hold that value that you read from the port.

Actually, there is no perhaps about it...

PaulS:
Perhaps that is because you are NOT using an int8_t variable to hold that value that you read from the port.

Actually, there is no perhaps about it...

yes you are right, thought i got away with that one.

Per's typo created a confusion. uint8_ is "unsigned 8 bit integer type" and "int8_t is signed 8 bit integer type"

Sorry about that. I meant to write int8_t but had a brain glitch.