Go Down

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

Mattix

Apr 11, 2012, 03:24 am
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.

#1
Apr 11, 2012, 03:52 am
I want the arduino to read that byte and then put that value into a char variable.

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

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

#2
Apr 11, 2012, 04:06 am
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.

#3
Apr 11, 2012, 04:11 am
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

#4
Apr 11, 2012, 04:16 am
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