Este dilema se puede resolver de variadas maneras, pero te enseñamos la respuesta más completa en nuestra opinión.
Ejemplo 1: convertir string para char c++
// "std::string" has a method called "c_str()" that returns a "const char*"// pointer to its inner memory. You can copy that "const char*" to a variable// using "strcpy()".
std::string str ="Hello World";char buffer[50];strcpy(buffer, str.c_str());
std::cout << buffer;//Output: Hello World//POSTED BY eferion ON STACK OVERFLOW (IN SPANISH).
Ejemplo 2: entero a char c++
// for example you have such integerint i =3;// and you want to convert it to a char so thatchar c ='3';
what you need to do is, by adding i to '0'. The reason why it works is because '0' actually means an integer value of 48.'1'..'9' means 49..57. This is a simple addition to find out corresponding character for an single decimal digit integer:
i.e.char c ='0'+ i;
If you know how to convert a single decimal digit int to char, whats left is how you can extract individual digit from a more-than-one-decimal-digit integer
it is simply a simple math by making use of /and%int i =123%10;// give u last digit, which is 3int j =123/10;// give remove the last digit, which is 12
The logic left is the homework you need to do then.
Ejemplo 3: C++ int a char*
std::string s = std::to_string(number);charconst*pchar = s.c_str();//use char const* as target type
valoraciones y reseñas
Si estás contento con lo expuesto, eres capaz de dejar una división acerca de qué le añadirías a esta crónica.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)