Storing an integer into a char* in C++ - c++

I'm writing some code that returns an integer, which then needs to be outputted using printw from the ncurses library. However, since printw only takes char*, I can't figure out how to output it.
Essentially, is there a way to store a integer into a char array, or output an integer using printw?

printw() accepts const char * as a format specifier. What you want is
printw("%d",yournumber);

The itoa function converts an int to char*.

Use itoa() or sprintf() to convert integer to ascii string.
Example:
char s[50];
sprintf(s, "%d", someInteger);
now u can pass s as char*

itoa will help you.

Related

Binary writing in C++, how to implement it?

Update: Still none game me answer to my main question how to save string char by char or as a whole (I want to ignore the last Null)?
Today I learned something new, which wasn't that clear to me.
I know how to save data as binary to a file, first I open it like this:
std::ofstream outfile(filename, std::ios_base::binary);
and then if I want to write a number I do the following:
outfile.write((const char*)&num, sizeof(int));
But, what about writing a string, how may I do this? char by char, or is there a faster method? Plus, what should the size of it be?
But, what about writing a string, how may I do this? char by char, or is there a faster method? Plus, what should the size of it be?
you can use the c_str() method in std::string to get the char array exist inside the string object, as it returns const char* and it's the same type in file.write() parameters. And for the size you can get the string size using length() method from std::string. the code can be like :
string mystr = "hello world";
outfile.write(mystr.c_str(), mystr.length());
And for
outfile.write((const char*)&num1, sizeof(unsigned int));
it save something but it is not your integer, it does not save it. you may try this :
outfile.write(reinterpret_cast<char*>(&num1), sizeof(num1));
and if it doesn't work you need to save your integer manually in a char array and write it to the file. you can convert your int to char using char* _itoa(int value, char* str, int base); and for char* str size you can allocate a number of chars as many digit you have in your integer.
PS: _itoa function belongs to C so using it in C++ may require to define _CRT_SECURE_NO_WARNINGS in the preprocessors

C++ append unsigned char to wstring

I want to append an unsigned char to a wstring for debugging reasons.
However, I don't find a function to convert the unsigned char to a wstring, so I can not append it.
Edit:
The solutions posted so far do not really do what I need.
I want to convert 0 to "0".
The solutions so far convert 0 to a 0 character, but not to a "0" string.
Can anybody help?
Thank you.
unsigned char SomeValue;
wstring sDebug;
sDebug.append(SomeValue);
The correct call for appending a char to a string (or in this case, a wchar_t to a wstring) is
sDebug.push_back(SomeValue);
Documentation here.
To widen your char to a wchar_t, you can also use std::btowc which will widen according to your current locale.
sDebug.push_back(std::btowc(SomeValue));
Just cast your unsigned char to char:
sDebug.append(1, static_cast<char>(SomeValue));
And if you want to use operator+ try this:
sDebug+= static_cast<char>(SomeValue);
Or even this:
sDebug+=boost::numeric_cast<char>(SomeValue);
There's an overload of append that also takes the number of times to append the given character:
sDebug.append(1, SomeValue);
However, this will result in a conversion between unsigned char and wchar_t. Perhaps you want SomeValue to be a wchar_t.
wstring has a constructor that takes a char. That would create a wstring from a char which you can then append.

C++ atoi with unsigned char

I got an unsigned char with the value of 1, I need to put this in a string like "1".
But if I try to put this directly into a stringstream it will get the value of char(1) and I need it to be "1".
I know that if I can get this into the function atoi it will return the "1" value.
But I tried to cast it to char and put it in the atoi function, but it throws an exception.
Already tried to put it in a string and them cast c_str() into atoi function, but without success yet.
If someone can help me I'll apreciate.
Simply cast a char to an int before inserting it into the std::stringstream:
ss << static_cast<int>(c);
This will treat the value of the char not as a character but as a numerical value.
I believe you're confusing two functions here.
You wish to convert an integer to a string.
atoi (ascii to integer) however takes a string and parses it into an integer, making "123" into 123.
You are looking for the itoa function here, which has this prototype:
char * itoa ( int value, char * str, int base );
In your case, this would look like this:
char Buf[10];
itoa(123, Buf, 10);
printf("%s", Buf); //Outputs 123
Please remember though, that itoa is not part of the standard, even though it is supported by some compilers. For a more standard-compliant version use:
sprintf(Buf, "%d", 123);
Of course all of this is plain C, but any C++ compiler will work with this all the same.
To convert a numeric value in the range [0,9] to the corresponding character, just add '0' to it. That's guaranteed to work on all systems.

c++ char question

so I'm implementing this chess program on C++ and I'm trying to integrate to winboard protocol...one of the functions that they say I need to write to do so should have the following signature:
char *MoveToText(MOVE move); // converts the move from your internal format to text like e2e2, e1g1, a7a8q.
my question is....the text formats are something like e2e2....but the return type of that function is char...and as far as I can understand it, char is just one single character....
so how come are they telling me to use this signature?
or am I mistaken and in fact char can also store multiple characters such as e2e2, e1g1 etc?
Yeah, in C, a char* points to an array of characters. C treats arrays of characters as strings, terminated by a null byte.
The return is a char* or a c-style string =)
char * is a pointer on char - address of sequence of characters.
It returns pointer to char, which is basically a c-string.
Take a look at this tutorial: http://www.cprogramming.com/tutorial/lesson9.html

How to put c++ string into char array

If I have something like
char name[10];
and I want to put the string s into name, where s = "joe"
how would I do that?
Also, can I make a function that takes strings as inputs, but treats those as char arrays?
strcpy (&name, s.c_str());
std::string has a c_str member that converts it to const char*. To copy from one char array to another use strcpy.
strcpy() will generally get the job done. strncpy() is better, if available.
If you have a C++ string, you can call its c_str() method to get a char *, suitable for using with strcpy(), defined in <cstring>.
std::memcpy(name, str.c_str(), str.size() + 1);