can't correctly convert
Sep 24, 2016 at 9:14am UTC
Hello,
I heave question about converting the integers to the char
with static_cast
there is simple code
1 2 3 4 5 6 7 8 9 10 11 12
#include<iostream>
int main()
{
int n;
for (n=25; n<35; n++){
std::cout << "The n= " << n << static_cast <char >(n);
std::cout << std::endl;
}
return 0;
}
but compiler shows something like that
http://i1019.photobucket.com/albums/af319/Stas1976/Untitled_zps6f5vrjpm.jpg
some sign convert but some don't
where is problem?
Last edited on Sep 24, 2016 at 9:20am UTC
Sep 24, 2016 at 10:49am UTC
Take a look at the ASCII characters you're casting to:
-
http://en.cppreference.com/w/cpp/language/ascii
-
http://www.cplusplus.com/doc/ascii/
The lower values (0 - 32) do not have an obvious character representation, such as escape and backspace. These are used for formatting/control rather than representing a letter or a number.
It looks like your compiler will print a box containing a question mark for chars that don't have a visual representation.
Sep 24, 2016 at 12:38pm UTC
You can convert integar to char like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<iostream>
#include <sstream>
int main()
{
int n;
char _n;
for (n=25; n<35; n++)
{
std::istringstream convert(n);
convert >> _n;
std::cout << "The n= " << n << _n;
std::cout << std::endl;
}
return 0;
}
Topic archived. No new replies allowed.