Reputation: 9
How to cast char=2 to char="2" ?
i need it to send via uart, but when im trying to send char as 2 i get nothing, but when i send as "2" i get 2
The point is, i have
int s=2;
and i need to write it to char as "2" not 2. i tried a few ways but always failure. when char = 2 message in terminal is just empty, when char is signed as "2" it works fine. When i tries to convert int to char , char was always signed as 2, i can't just send int via uart becouse block sending function needs pointer.
Upvotes: 0
Views: 2027
Reputation: 1990
What is the function head for the the call you use to send via the uart? Can you give us any more information about what the device is?
You can fix how you send on one end, or you can change how you read on the other end. Presumably you can read data as integers, or cast it, so not everything has to be sent via strings, and this whole conversion problem would go away.
All this being said, you can convert 0-9 from integer into ascii code by adding a constant 48, but I don't think that's really what you're going for.
Upvotes: 0
Reputation:
If you need only one char at once, You can use the following too:
char s =2;
s+='0';
Upvotes: 0
Reputation: 13558
you can use itoa()
to convert an integer to a char.
http://www.jb.man.ac.uk/~slowe/cpp/itoa.html
Upvotes: 1