How to output a character as an integer through cout?
char a = 0xab;
cout << +a; // promotes a to a type printable as a number, regardless of type.
This works as long as the type provides a unary +
operator with ordinary semantics. If you are defining a class that represents a number, to provide a unary + operator with canonical semantics, create an operator+()
that simply returns *this
either by value or by reference-to-const.
source: Parashift.com - How can I print a char as a number? How can I print a char* so the output shows the pointer's numeric value?
How can I display an integer variable as thier intended ASCII letters in `cout` when parsing a binary file?
There is no formatting spec like std::ascii
but there is a string
constructor you can use:
std::string int2str((char*)&n32Bits, 4);
std::cout << "n32Bits: " << int2str << std::endl;
This constructor takes a char
buffer and length.
A character gives integer as output if we input an integer into it but this doesn't happen when an integer is assigned to it. Why?
While you assign an integer to the character variable, it reads the integer from the memory and store at its location and while interpreting that char value, it returns ASCII equivalent of it. While reading cin buffer into the character variable(char) it reads 1 byte of the char or its ASCII value in the memory and gives the output as ASCII equivalent of it.
Cast element of character array to integer, and print using cout
Remember that the type of argv
is char* argv[]
, so argv[1]
is not a single char
, but a C-style string.
To print the first character, use argv[1][0]
.
std::cout << "(int)argv[1][0] : " << (int)argv[1][0] << std::endl;
How can I convert an array of characters to an integer (for 2 or more digits)
I think your professor wants you to read in text, and then convert that text to a number. This is a silly requirement.
The sensible program would >>
into int
s directly
int coeff;
int expo;
std::cin >> coeff >> expo;
To read it into intermediate text safely you could
std::string coeff_s;
std::string expo_s;
std::cin >> coeff_s >> expo_s;
int coeff = std::stoi(coeff_s);
int expo = std::stoi(expo_s);
However I predict that your professor will insist on using raw char[]
and not std::string
. You have to be more careful (prior to C++20) with that, specifying the length of the char[]
.
char coeff_s[20];
char expo_s[20];
std::cin >> std::setw(20) >> coeff_s >> std::setw(20) >> expo_s;
int coeff = std::atoi(coeff_s);
int expo = std::atoi(expo_s);
Related Topics
Right Way to Split an Std::String into a Vector≪String≫
Displaying the #Include Hierarchy For a C++ File in Visual Studio
Error Lnk2019: Unresolved External Symbol _Winmain@16 Referenced in Function _Tmaincrtstartup
Overloading Member Access Operators -≫, .*
Deprecated Header ≪Codecvt≫ Replacement
Best Way to Store Currency Values in C++
How to Convert a Double into a String in C++
How to Use a C++ String in a Structure When Malloc()-Ing the Same Structure
Forward Declaration of a Typedef in C++
C++, What Does the Colon After a Constructor Mean
Why Does Integer Overflow on X86 With Gcc Cause an Infinite Loop
How to Hide a String in Binary Code
Running My Program Says "Bash: ./Program Permission Denied"