0

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.

0

3 Answers 3

1

you can use itoa() to convert an integer to a char.

http://www.jb.man.ac.uk/~slowe/cpp/itoa.html

Sign up to request clarification or add additional context in comments.

8 Comments

tried but compilator said that there is no itoa function in stdlib.h this app is not writen for pc.
There is. So You probably tried to use it with invalid parameters. cplusplus.com/reference/cstdlib/itoa
undefined reference to 'itoa' , #include <stdlib.h>
I looked it up. Itoa is not defined in ANSI C, but is supported by some compilers. If it doesn't work, write it for Yourself like it is on the linked page
how can i write it for my self if i can't make this work, sry im a little bit newmbie in C.
|
0

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

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

1 Comment

ussualy there are over a dozen chars.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.