Generating a char using input from user in C++ [duplicate] - c++

This question already has answers here:
How to convert a std::string to const char* or char*
(11 answers)
Closed 7 years ago.
I wish to make a char with digits between 0-9. The user decides how many digits to use.
For example, if the user inputs 4, the char should be 01234.
Please note I cannot use the string data type. I have to use char.
I know how to generate a string for the same logic but not a char.
So if there is a way to convert string to char, that will work well. I tried
string randomString; //this contains the set of numbers 0-9 on the basis of the users input
char charString = randomString;
This however does not work.

So if there is a way to convert string to char
Yes, it's called a character array and you can easily convert a string type to a character array like so:
const char* charString = randomString.c_str();
You can find more information about c_str() method here and you should review this material regarding character arrays.
If you require a non-const (can be modified) character array, refer to the above links which will explain it and actually give examples about how to accomplish that.

Related

Why are are these re-cast string types returning different data? [duplicate]

This question already has answers here:
How does "\x" work in a String?
(4 answers)
Closed 1 year ago.
I'm reading a 'qbytearray' string from a text file that I don't have control over.
I'm not using Qt.
But this way of handing the string in the text file works in my program:
unsigned char* apa = (unsigned char*)"\xf1`\xf9\a\\\x9cT\x82z\x17\x5\xb9\xbc\x60\xca\x15";
but this does not work:
unsigned char* apa = (unsigned char*)settings_in.at(1).c_str();
(yep I have double checked the contents of the std::string settings_in)
Why are these different?
And how could I achieve the top result with a std::string?
UPDATE: Ok so based on your comments here's more info:
I'm reading a Qt generated textfile with the line,
1\rp_key=#ByteArray(\xf1`\xf8\a\\\x9cT\x82z\x14\x5\xb9\xbc\x80\xca\x15)
I'm stripping out the long string inside the parenthesis.
I'd love to not recast but Iv'e tried so many things that hasn't worked and that seemed to get me somewhere.
The goal is to fill in this variable,
uint8_t rp_key[0x10] = {0};
The hard coded 'ground truth':
unsigned char* apa = (unsigned char*)"\xf1`\xf8\a\\\x9cT\x82z\x14\x5\xb9\xbc\x80\xca\x15";
printf("No1: %s\n", apa);
and std::string code that prints out the same string as above but in ascii (unicode?) format:
std::string str = settings_in.at(1);
unsigned char* thing = (unsigned char*)str.c_str();
printf("No2: %s\n", thing);
Hm I'm not sure what I can write that proves the contents of my 'settings_in'. I just tried this:
char *cstr = "\xf1`\xf8\a\\\x9cT\x82z\x18\x5\xb9\xbc\x80\xca\x15";
std::string str = cstr;
unsigned char* thing = (unsigned char*)str.c_str();
printf("No2: %s\n", thing);
but that's no good as the result works in my program. Is that proof?
(please ignore the actual contents as I'm altering it since the original is sensitive)
This also works in the program,
std::string str = "\xf1`\xf8\a\\\x7cT\x82z\x14\x5\xb9\xbc\x80\xca\x15";
unsigned char* thing = (unsigned char*)str.c_str();
printf("No2: %s\n", thing);
I'm going to put this here as the answer from another overflow Question:
The translation is done at compile-time so that every string you
manually enter into the source code with \x and such ends up being the
character it represents in the binary. If you want to do this at
run-time you will need to invoke a parse function like strtol() using
base 16 passing the string containing the hex and cast it to a char.
How does "\x" work in a String?
This matches perfectly what's happening.
The suggestion of how to emulate at runtime doesn't seem to apply to my case though.

Character Pointer and string variable

I tried to use a character Pointer go throw string character (iterate) but I found i can not say the below:
string Name = "Hello";
char *ch = Name;
like the previous statements i am getting error during execution.
However when I am doing like that:
char *ch = "Hello";
the program running without throwing any exception.
Why is that?
I have recently encountered similar problem and the simplest answer is that std::string is a different type from char*, more precisely std::string is an object which contains some characters (your text) and few methods, which allow you to do multiple operations with your text. You can imagine creating a class Integer for storing the value, but also a method allowing you to square and cube the number which is stored in the Ingerer class. Even though they could store the same numerical value, you will not be able to compare them (unless you overload the operator==), as their types are different.
If you wish to use the code you provided, you need to rewrite the second line as
const char *ch = Name.c_str();
it is allowed because std::string contains a method c_str() which "casts" itself to const char*. If you want to learn more about strings, be sure to visit C++ reference about strings.

c++ convert a word with 4 character stored as char* to an int and vice versa [duplicate]

This question already has answers here:
Using bitwise operators in C++ to change 4 chars to int
(2 answers)
Closed 8 years ago.
I'm searching for a way to convert a word with 4 characters stored as char* to an int and vice versa because i want to transfer the char* through a function which needs an int as argument. Here's an example:
char* word = "abcd";
int number;
// write word in number
char* word2;
// write number in word2
At the end word2 should be the same as word. It would also help me if you know how to convert it in one direction only.
Assuming it will be converted back on a system with the same endianness
number=*((int*)word);
Convert back:
char word2[5];
*((int*)word2) = number;
word2[4]=0;

difference between char array and string in cplusplus [duplicate]

This question already has answers here:
What is the difference between a Character Array and a String?
(10 answers)
Closed 9 years ago.
I want to know the difference between character array and string in c++.
Can any one answer to this??
Please,
Thanks
Vishnukumar
string is a class/object, with methods and encapsulated data.
A char array is simply a contiguous block of memory meant to hold chars.
(1) char array is just a block of char type data:
e.g. char c[100]; // 100 continuous bytes are allotted to c
(2a) By string, if you mean char string then, it's little similar to array but it's allocated in the readonly segment of the memory and should be assigned to a const char*:
e.g. const char *p = "hello"; // "hello" resides in continuous character buffer
[note: char c[] = "hello"; belongs to category (1) and not to (2a)]
(2b) By string if yo umean std::string then, it's a standard library class from header and you may want to refer its documentation or search on web

c++ char to const char* [duplicate]

This question already has answers here:
C++ convert char to const char*
(5 answers)
Closed 10 years ago.
I need to convert a character in a character array to a const char * in order to print it to a file using fstream. I'm not sure exactly how to do so. I've tried putting the single char into a string, then using c_str(), but that does not work..
If you want to write a single character, just use operator<<:
char arr[256] = "...";
fstream f(...);
f << arr[2];
You don't need to convert the character to a C string.
Hm... If you have a character array, that already decays into char * when passed to a function.
If you need only one character:
char array[128]; // whatever - you want to extract the char from this
char s[] = { array[64], 0 };
then use s which now can decay into char *.
Edit: D'oh, I just read this:
in order to print it to a file using fstream
Well, then don't bother converting it to a proper C string. operator<< knows its job, and it's overloaded for char too.