âchar inChar = (char)Serial.read();â why is the âcharâ in brackets? and what is it called

Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I am sure this is a very simple answer, but I don't know to look it up on google
In the example "SerialEvent" in the arduino IDE (v1.8.7), there is the following function
void serialEvent()
while (Serial.available())
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == 'n')
stringComplete = true;
On the third line "char inChar = (char)Serial.read();" the (char) is given in brackets before the serial read.
Why is this? and more specifically how is called ?
serial
 |Â
show 1 more comment
up vote
2
down vote
favorite
I am sure this is a very simple answer, but I don't know to look it up on google
In the example "SerialEvent" in the arduino IDE (v1.8.7), there is the following function
void serialEvent()
while (Serial.available())
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == 'n')
stringComplete = true;
On the third line "char inChar = (char)Serial.read();" the (char) is given in brackets before the serial read.
Why is this? and more specifically how is called ?
serial
5
This is actually more a C/C++ language question. The hint is "Please check the return value type forSerial.read()".
â Mikael Patel
Sep 24 at 21:04
So it basically forces the value returned from serial.read() to be a char? Do you know how this is named, so I can look up the documentation?
â will.mendil
Sep 24 at 21:17
In C, why are there parentheses around (int) in this example?, Type in parentheses in C variable definition
â phuclv
Sep 25 at 1:58
What topics can I ask about here? On topic: Specific questions about Arduino boards, code⦠arduino.stackexchange.com/help/on-topic. Is this question not a specific question about Arduino code? Voting to not close.
â VE7JRO
Sep 25 at 5:33
@will.mendil This is called "Explicit type conversion" or "Casting". en.cppreference.com/w/cpp/language/explicit_cast. In some cases it ends up as some extra code but in other cases it is just a hint to the compiler and no extra code is generated.
â Mikael Patel
Sep 25 at 10:34
 |Â
show 1 more comment
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am sure this is a very simple answer, but I don't know to look it up on google
In the example "SerialEvent" in the arduino IDE (v1.8.7), there is the following function
void serialEvent()
while (Serial.available())
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == 'n')
stringComplete = true;
On the third line "char inChar = (char)Serial.read();" the (char) is given in brackets before the serial read.
Why is this? and more specifically how is called ?
serial
I am sure this is a very simple answer, but I don't know to look it up on google
In the example "SerialEvent" in the arduino IDE (v1.8.7), there is the following function
void serialEvent()
while (Serial.available())
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == 'n')
stringComplete = true;
On the third line "char inChar = (char)Serial.read();" the (char) is given in brackets before the serial read.
Why is this? and more specifically how is called ?
serial
serial
asked Sep 24 at 20:56
will.mendil
132
132
5
This is actually more a C/C++ language question. The hint is "Please check the return value type forSerial.read()".
â Mikael Patel
Sep 24 at 21:04
So it basically forces the value returned from serial.read() to be a char? Do you know how this is named, so I can look up the documentation?
â will.mendil
Sep 24 at 21:17
In C, why are there parentheses around (int) in this example?, Type in parentheses in C variable definition
â phuclv
Sep 25 at 1:58
What topics can I ask about here? On topic: Specific questions about Arduino boards, code⦠arduino.stackexchange.com/help/on-topic. Is this question not a specific question about Arduino code? Voting to not close.
â VE7JRO
Sep 25 at 5:33
@will.mendil This is called "Explicit type conversion" or "Casting". en.cppreference.com/w/cpp/language/explicit_cast. In some cases it ends up as some extra code but in other cases it is just a hint to the compiler and no extra code is generated.
â Mikael Patel
Sep 25 at 10:34
 |Â
show 1 more comment
5
This is actually more a C/C++ language question. The hint is "Please check the return value type forSerial.read()".
â Mikael Patel
Sep 24 at 21:04
So it basically forces the value returned from serial.read() to be a char? Do you know how this is named, so I can look up the documentation?
â will.mendil
Sep 24 at 21:17
In C, why are there parentheses around (int) in this example?, Type in parentheses in C variable definition
â phuclv
Sep 25 at 1:58
What topics can I ask about here? On topic: Specific questions about Arduino boards, code⦠arduino.stackexchange.com/help/on-topic. Is this question not a specific question about Arduino code? Voting to not close.
â VE7JRO
Sep 25 at 5:33
@will.mendil This is called "Explicit type conversion" or "Casting". en.cppreference.com/w/cpp/language/explicit_cast. In some cases it ends up as some extra code but in other cases it is just a hint to the compiler and no extra code is generated.
â Mikael Patel
Sep 25 at 10:34
5
5
This is actually more a C/C++ language question. The hint is "Please check the return value type for
Serial.read()".â Mikael Patel
Sep 24 at 21:04
This is actually more a C/C++ language question. The hint is "Please check the return value type for
Serial.read()".â Mikael Patel
Sep 24 at 21:04
So it basically forces the value returned from serial.read() to be a char? Do you know how this is named, so I can look up the documentation?
â will.mendil
Sep 24 at 21:17
So it basically forces the value returned from serial.read() to be a char? Do you know how this is named, so I can look up the documentation?
â will.mendil
Sep 24 at 21:17
In C, why are there parentheses around (int) in this example?, Type in parentheses in C variable definition
â phuclv
Sep 25 at 1:58
In C, why are there parentheses around (int) in this example?, Type in parentheses in C variable definition
â phuclv
Sep 25 at 1:58
What topics can I ask about here? On topic: Specific questions about Arduino boards, code⦠arduino.stackexchange.com/help/on-topic. Is this question not a specific question about Arduino code? Voting to not close.
â VE7JRO
Sep 25 at 5:33
What topics can I ask about here? On topic: Specific questions about Arduino boards, code⦠arduino.stackexchange.com/help/on-topic. Is this question not a specific question about Arduino code? Voting to not close.
â VE7JRO
Sep 25 at 5:33
@will.mendil This is called "Explicit type conversion" or "Casting". en.cppreference.com/w/cpp/language/explicit_cast. In some cases it ends up as some extra code but in other cases it is just a hint to the compiler and no extra code is generated.
â Mikael Patel
Sep 25 at 10:34
@will.mendil This is called "Explicit type conversion" or "Casting". en.cppreference.com/w/cpp/language/explicit_cast. In some cases it ends up as some extra code but in other cases it is just a hint to the compiler and no extra code is generated.
â Mikael Patel
Sep 25 at 10:34
 |Â
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
5
down vote
accepted
It is called type casting and will convert one type to another. The return type of Serial.read() is int, because it will return -1, when you try to read without any bytes being available. The casting with (char) will drop the high byte of the (2-byte long) integer, leaving only the lower byte. This byte will contain the actual read data, in which you are interested here.
Casting is a complex thing, because there can be different ways to convert different types (for example maintaining the bit-content vs. maintaining the decimal value). The casting, that you are seeing here, should only be done, if you really know, what you are doing. The bits will not be changed to maintain the correct value, but the bit-content will be preserved, unless the new type is smaller than the former. In this case the rest is lost. Also, if you try to cast a float value to an int value with this, you will get a totally different decimal value out of it, because the numbers are encoded in the bits in a totally different way. Here it is known to be OK, especially, if you are checking for available bytes before reading with Serial.available(), because the library puts the ASCII data in the lower byte.
For further explanations you should consult C/C++ guides.
1
Thank you ever so much !
â will.mendil
Sep 24 at 21:40
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
It is called type casting and will convert one type to another. The return type of Serial.read() is int, because it will return -1, when you try to read without any bytes being available. The casting with (char) will drop the high byte of the (2-byte long) integer, leaving only the lower byte. This byte will contain the actual read data, in which you are interested here.
Casting is a complex thing, because there can be different ways to convert different types (for example maintaining the bit-content vs. maintaining the decimal value). The casting, that you are seeing here, should only be done, if you really know, what you are doing. The bits will not be changed to maintain the correct value, but the bit-content will be preserved, unless the new type is smaller than the former. In this case the rest is lost. Also, if you try to cast a float value to an int value with this, you will get a totally different decimal value out of it, because the numbers are encoded in the bits in a totally different way. Here it is known to be OK, especially, if you are checking for available bytes before reading with Serial.available(), because the library puts the ASCII data in the lower byte.
For further explanations you should consult C/C++ guides.
1
Thank you ever so much !
â will.mendil
Sep 24 at 21:40
add a comment |Â
up vote
5
down vote
accepted
It is called type casting and will convert one type to another. The return type of Serial.read() is int, because it will return -1, when you try to read without any bytes being available. The casting with (char) will drop the high byte of the (2-byte long) integer, leaving only the lower byte. This byte will contain the actual read data, in which you are interested here.
Casting is a complex thing, because there can be different ways to convert different types (for example maintaining the bit-content vs. maintaining the decimal value). The casting, that you are seeing here, should only be done, if you really know, what you are doing. The bits will not be changed to maintain the correct value, but the bit-content will be preserved, unless the new type is smaller than the former. In this case the rest is lost. Also, if you try to cast a float value to an int value with this, you will get a totally different decimal value out of it, because the numbers are encoded in the bits in a totally different way. Here it is known to be OK, especially, if you are checking for available bytes before reading with Serial.available(), because the library puts the ASCII data in the lower byte.
For further explanations you should consult C/C++ guides.
1
Thank you ever so much !
â will.mendil
Sep 24 at 21:40
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
It is called type casting and will convert one type to another. The return type of Serial.read() is int, because it will return -1, when you try to read without any bytes being available. The casting with (char) will drop the high byte of the (2-byte long) integer, leaving only the lower byte. This byte will contain the actual read data, in which you are interested here.
Casting is a complex thing, because there can be different ways to convert different types (for example maintaining the bit-content vs. maintaining the decimal value). The casting, that you are seeing here, should only be done, if you really know, what you are doing. The bits will not be changed to maintain the correct value, but the bit-content will be preserved, unless the new type is smaller than the former. In this case the rest is lost. Also, if you try to cast a float value to an int value with this, you will get a totally different decimal value out of it, because the numbers are encoded in the bits in a totally different way. Here it is known to be OK, especially, if you are checking for available bytes before reading with Serial.available(), because the library puts the ASCII data in the lower byte.
For further explanations you should consult C/C++ guides.
It is called type casting and will convert one type to another. The return type of Serial.read() is int, because it will return -1, when you try to read without any bytes being available. The casting with (char) will drop the high byte of the (2-byte long) integer, leaving only the lower byte. This byte will contain the actual read data, in which you are interested here.
Casting is a complex thing, because there can be different ways to convert different types (for example maintaining the bit-content vs. maintaining the decimal value). The casting, that you are seeing here, should only be done, if you really know, what you are doing. The bits will not be changed to maintain the correct value, but the bit-content will be preserved, unless the new type is smaller than the former. In this case the rest is lost. Also, if you try to cast a float value to an int value with this, you will get a totally different decimal value out of it, because the numbers are encoded in the bits in a totally different way. Here it is known to be OK, especially, if you are checking for available bytes before reading with Serial.available(), because the library puts the ASCII data in the lower byte.
For further explanations you should consult C/C++ guides.
answered Sep 24 at 21:32
chrisl
2,1212311
2,1212311
1
Thank you ever so much !
â will.mendil
Sep 24 at 21:40
add a comment |Â
1
Thank you ever so much !
â will.mendil
Sep 24 at 21:40
1
1
Thank you ever so much !
â will.mendil
Sep 24 at 21:40
Thank you ever so much !
â will.mendil
Sep 24 at 21:40
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f56359%2fchar-inchar-charserial-read-why-is-the-char-in-brackets-and-what-is%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
5
This is actually more a C/C++ language question. The hint is "Please check the return value type for
Serial.read()".â Mikael Patel
Sep 24 at 21:04
So it basically forces the value returned from serial.read() to be a char? Do you know how this is named, so I can look up the documentation?
â will.mendil
Sep 24 at 21:17
In C, why are there parentheses around (int) in this example?, Type in parentheses in C variable definition
â phuclv
Sep 25 at 1:58
What topics can I ask about here? On topic: Specific questions about Arduino boards, code⦠arduino.stackexchange.com/help/on-topic. Is this question not a specific question about Arduino code? Voting to not close.
â VE7JRO
Sep 25 at 5:33
@will.mendil This is called "Explicit type conversion" or "Casting". en.cppreference.com/w/cpp/language/explicit_cast. In some cases it ends up as some extra code but in other cases it is just a hint to the compiler and no extra code is generated.
â Mikael Patel
Sep 25 at 10:34