user2304765
user2304765

Reputation: 9

C converting value char to string char

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

Answers (3)

Macattack
Macattack

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

user1300630
user1300630

Reputation:

If you need only one char at once, You can use the following too:

char s =2;
s+='0'; 

Upvotes: 0

Gung Foo
Gung Foo

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

Related Questions