Go Down

Topic: Byte to Char (Read 347 times) previous topic - next topic

Mattix

I'm sending a byte on a C# side (over a serial port) over to the arduino.  I want the arduino to read that byte and then put that value into a char variable.  My question is, if the value is greater than 127/128 will it be positive and if it's less than that, will it be negative in the new char?

Example:

If I read a byte (value = 255) and I put that value into a character, will it be 127 and if I have another byte (value = 0) will that value become -128?  Thank you very much for the help.

Coding Badly

I want the arduino to read that byte and then put that value into a char variable.  


Code: [Select]
if ( Serial.available() )
{
 char ch;
 ch = Serial.read();
}


Got it.

Quote
My question is, if the value is greater than 127 /128 will it be positive


No.  An unsigned eight bit value greater than 127 is interpreted as a negative signed value.

Quote
and if it's less than that {128}, will it be negative in the new char?


No.  An unsigned eight bit value less than or equal to 127 is interpreted as a positive signed value.

Quote
If I read a byte (value = 255) and I put that value into a character, will it be 127


No.  An unsigned eight bit value of 255 when converted to a signed value is interpreted as a -1.

Quote
and if I have another byte (value = 0) will that value become -128?


No.  Zero is zero.

Maybe this will help...

[font=Courier New]   Uns   Hex   Sig
 ----  ----  ----
    0  0x00     0
    1  0x01    +1
    2  0x02    +2
  ...   ...   ...
  126  0x7E  +126
  127  0x7F  +127
  128  0x80  -128
  129  0x81  -127
  130  0x82  -126
  ...   ...   ...
  254  0xFE    -2
  255  0xFF    -1[/font]

Mattix

Ok thank you very much. I think I can use the map function to solve my problem. Have a good day!

Also, I understood hex, just kind of confused what would happen going from signed to unsigned. You cleared that up.

Coding Badly

Ok thank you very much.


You are welcome.

Quote
I think I can use the map function to solve my problem.


If you describe the problem I suspect someone here can offer a simpler solution.

Quote
Have a good day!


You too!

Mattix

It's nothing too bad.  I don't think I'll have further difficulties, but if I do, I'll post here in a few minutes.

Go Up