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

The name of the pictureThe name of the pictureThe name of the pictureClash 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 ?










share|improve this question







New contributor




will.mendil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    This is actually more a C/C++ language question. The hint is "Please check the return value type for Serial.read()".
    – Mikael Patel
    3 hours ago










  • 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
    3 hours ago















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 ?










share|improve this question







New contributor




will.mendil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    This is actually more a C/C++ language question. The hint is "Please check the return value type for Serial.read()".
    – Mikael Patel
    3 hours ago










  • 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
    3 hours ago













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 ?










share|improve this question







New contributor




will.mendil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question







New contributor




will.mendil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




will.mendil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




will.mendil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 4 hours ago









will.mendil

132




132




New contributor




will.mendil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





will.mendil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






will.mendil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1




    This is actually more a C/C++ language question. The hint is "Please check the return value type for Serial.read()".
    – Mikael Patel
    3 hours ago










  • 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
    3 hours ago













  • 1




    This is actually more a C/C++ language question. The hint is "Please check the return value type for Serial.read()".
    – Mikael Patel
    3 hours ago










  • 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
    3 hours ago








1




1




This is actually more a C/C++ language question. The hint is "Please check the return value type for Serial.read()".
– Mikael Patel
3 hours ago




This is actually more a C/C++ language question. The hint is "Please check the return value type for Serial.read()".
– Mikael Patel
3 hours ago












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
3 hours ago





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
3 hours ago











1 Answer
1






active

oldest

votes

















up vote
2
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.






share|improve this answer
















  • 1




    Thank you ever so much !
    – will.mendil
    3 hours ago










Your Answer





StackExchange.ifUsing("editor", function ()
return StackExchange.using("schematics", function ()
StackExchange.schematics.init();
);
, "cicuitlab");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "540"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






will.mendil is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















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






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
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.






share|improve this answer
















  • 1




    Thank you ever so much !
    – will.mendil
    3 hours ago














up vote
2
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.






share|improve this answer
















  • 1




    Thank you ever so much !
    – will.mendil
    3 hours ago












up vote
2
down vote



accepted







up vote
2
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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered 3 hours ago









chrisl

2,0512211




2,0512211







  • 1




    Thank you ever so much !
    – will.mendil
    3 hours ago












  • 1




    Thank you ever so much !
    – will.mendil
    3 hours ago







1




1




Thank you ever so much !
– will.mendil
3 hours ago




Thank you ever so much !
– will.mendil
3 hours ago










will.mendil is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















will.mendil is a new contributor. Be nice, and check out our Code of Conduct.












will.mendil is a new contributor. Be nice, and check out our Code of Conduct.











will.mendil is a new contributor. Be nice, and check out our Code of Conduct.













 


draft saved


draft discarded














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













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

Installing NextGIS Connect into QGIS 3?

One-line joke