| | |
reading a hexadecimal number
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
C++ Syntax (Toggle Plain Text)
#include <iostream.h> void main() { int n; while (cin >> n) { cout << "decimal: " << n << endl; //--- Print hex with leading zeros cout << "hex : "; for (int i=2*sizeof(int) - 1; i>=0; i--) { cout << "0123456789ABCDEF"[((n >> i*4) & 0xF)]; } cout << endl << endl; } }
That's reading a int and converting it to hex. Can u figure out how to do da opposite?
*Voted best profile in the world*
>how can you read a hexadecimal number in c++? is there any option with cin/
The std::hex modifier works with input streams:
Type in 1B (or 1b), and the output will be 27, which is a correct conversion from hexadecimal input to decimal output.
>That's reading a int and converting it to hex.
That's evil.
The std::hex modifier works with input streams:
C++ Syntax (Toggle Plain Text)
#include <iostream> int main() { int x; std::cin>> std::hex >> x; std::cout<< x <<'\n'; }
>That's reading a int and converting it to hex.
That's evil.
I'm here to prove you wrong.
•
•
Join Date: Jan 2006
Posts: 40
Reputation:
Solved Threads: 2
[QUOTE=Narue]>how can you read a hexadecimal number in c++? is there any option with cin/
The std::hex modifier works with input streams:
Type in 1B (or 1b), and the output will be 27, which is a correct conversion from hexadecimal input to decimal output.
Thank you
Beulah
The std::hex modifier works with input streams:
C++ Syntax (Toggle Plain Text)
#include <iostream> int main() { int x; std::cin>> std::hex >> x; std::cout<< x <<'\n'; }
Thank you
Beulah
![]() |
Similar Threads
- What's the definition of "five-digit" number? (C++)
- C: - reading in from text (C++)
- Reading Hexadecimal files using Visual Basic 6 (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: The New And Delete Function
- Next Thread: Anyone could help me....
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






