I'm just trying to learn more about coding -- typecasting in particular, as it seems to be used a lot in C++ coding in general.
So i'm trying to typecast an int into a char for std::cout to print:
[t]http://puu.sh/k5GPh/0646667ac6.png[/t]
As you can see, it just leaves a '?' where '25' should be. Why won't it work?
If you want to print the numeric value of a signed 8-bit integer you have to do this:
[code]
std::cout << +(char)32 << std::endl;
[/code]
Don't ask me why this works.
[QUOTE=tyguy;48647559]As you can see, it just leaves a '?' where '25' should be. Why won't it work?[/QUOTE]
It works just fine, you are converting an integer value into a character and printing that. Try the opposite for example, casting 'a' to an integer and printing (and then adding 1 and casting back to char and printing).
What you are actually printing is the symbol with the ASCII-value 25, which is not a printable character (hence the "?"), so the casting is working correctly.
If you want to convert the 25 to a char* (since 1 char is not enough to hold the two characters in 25), have a look at itoa or at stringstream for amore C++-way.
[QUOTE=Anven11;48647626]If you want to print the numeric value of a signed 8-bit integer you have to do this:
[code]
std::cout << +(char)32 << std::endl;
[/code]
Don't ask me why this works.[/QUOTE]
I find it easier to remember if I know why it works, so I looked it up:
[url]https://isocpp.org/wiki/faq/input-output#print-char-or-ptr-as-number[/url]
[QUOTE=gummiwipfel;48647728]What you are actually printing is the symbol with the ASCII-value 25, which is not a printable character (hence the "?"), so the casting is working correctly.
If you want to convert the 25 to a char* (since 1 char is not enough to hold the two characters in 25), have a look at itoa or at stringstream for amore C++-way.[/QUOTE]
In addition here is a ASCII chart to show you what you are trying to print when converting to a char :
[thumb]https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/ASCII-Table-wide.svg/2000px-ASCII-Table-wide.svg.png[/thumb]
I mean
[code]std::cout << static_cast<wchar_t>(25) << std::endl;[/code]
Is this not an option/what you wanted?
Output 25 for me
EDIT:
In fact
[code]std::cout << 25 << std::endl;[/code]
Works just fine
His cast works just fine, it's just that the ASCII value for 25 does not correlate to a human-readable character.
Understanding ASCII gets you actually pretty far when it comes to C++.
Since you're starting out programming, I would recommend attempting to implement your own function to reformat a binary number into base 10 (bonus point: any given base up to, say, 36)
Non windows user here: wtf is _tmain and _TCHAR
[QUOTE=proboardslol;48709004]Non windows user here: wtf is _tmain and _TCHAR[/QUOTE]
[url]https://msdn.microsoft.com/en-us/library/6wd819wh%28v=vs.140%29.aspx[/url]
[QUOTE=gummiwipfel;48647728]What you are actually printing is the symbol with the ASCII-value 25, which is not a printable character (hence the "?"), so the casting is working correctly.
If you want to convert the 25 to a char* (since 1 char is not enough to hold the two characters in 25), have a look at itoa or at stringstream for amore C++-way.[/QUOTE]
Or, even better, [url=http://en.cppreference.com/w/cpp/string/basic_string/to_string]std::to_string[/url].
Sorry, you need to Log In to post a reply to this thread.