Pages: [1]   Go Down
Author Topic: Byte to Char  (Read 318 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 206
Posts: 12859
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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...

  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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 206
Posts: 12859
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: