Getting hex through Cin
I believe in order to use hex you need to do something like this:
cin >> hex >> x;
cout << hex << x;
you can also replace hex with dec and oct etc.
Reliably get user hex input in C++ with error handling
cin.ignore()
will ignore the last character in its internal buffer. You need to use cin.ignore(std::numeric_limits<std::streamsize>::max())
to reset back to the last EOF. Then you can check the next input reliably.
After trying to work this one out (good practice with streams :D), I have come up with a solution
int startAddr;
std::cin.exceptions(std::ios_base::failbit);
while (true) {
std::cout << "Enter start addr: ";
try {
std::cin >> std::hex >> startAddr;
break;
} catch(std::exception&) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
std::cout << "Invalid number\n";
}
}
This makes use of the ability to set a failbit as an exception for std::iostream
s meaning we are guaranteed to catch the error and std::numeric_limits
as a more reliable way to clear the buffer
Using C++ hex and cin
The hex manipulator only controls how a value is read - it is always stored using the same internal binary representation. There is no way for a variable to "remember" that it was input in hex.
inputing hex and reading hex with C++
You seem to be intending to read and write one character at a time - cin and cout are both capable of working with integral values and this would be ideal here. Keep in mind that hexadecimal strings do represent numbers! Streams are capable of interpreting them as such. Simply changing the variables v and print to type int will allow you to read in an entire hexadecimal value at once.
For an example:
int value;
cin >> hex >> value;
cout << hex << value;
How to validate hex input with std::cin?
I guess I'd do something like this:
std::getline(cin, your_string);
if (your_string.find_first_not_of("0123456789abcdefABCDEF") != std::string::npos)
error("Non-hexadecimal input");
else {
std::istringstream is(your_string);
is >> std::hex >> address1;
}
There are a lot of variations, but the three steps I'd follow would be:
- read the whole line,
- validate it
- convert if good.
Getting wrong hex value from read function
d0
is a negative 8 bit value in signed char. This is exact the same negative value ffffffd0
in int. For getting "d0" in output, cast the signed char to another unsigned type of the same size (1 byte) then cast the result to int:
hexValue << hex << (int)(unsigned char)buffer[i];
hexValue << hex << (int)(uint8_t)buffer[i];
The first type cast keeps 8 bits in an unsigned type, a positive number, the second cast makes a positive int.
Read file in binary and output in hex std::cin to std::cout
The input stream operator typically requires a separator, you should read a buffer from the file, something like shown here: http://www.cplusplus.com/reference/iostream/istream/read/, then iterate over the contents and print out, you may also want to use showbase
to make the hex output prettier...
EDIT: try something like this:
char c;
while(std::cin.get(c))
...
How to input hex values instead of chars as input for boost::asio::buffer
That is your solution:
just replace
std::string hex_chars("E8 48 D8 FF FF 8B 0D");
with
std::string hex_chars;
std::getline(std::cin, hex_chars);
P.S. I hope I've understood your question correctly :)
Related Topics
Multiple Implicit Conversions on Custom Types Not Allowed
How to Do Input Validation in C++ with Cin
Does Case-Switch Work Like This
How to Get the Mouse Position in a Console Program
What Does "Void *(*)(Void *)" Mean in C++
Base Pointer to Array of Derived Objects
How to Show Enter Password in the Form of Asterisks(*) on Terminal
Why Does Nvcc Fails to Compile a Cuda File with Boost::Spirit
How to Use Non-Default Delimiters When Reading a Text File with Std::Fstream
Initializing a C++ Std::Istringstream from an in Memory Buffer
Training Custom Svm to Use with Hogdescriptor in Opencv
Create a Reverse Linkedlist in C++ from a Given Linkedlist
Why Does My Program Hang When Opening a Mkfifo-Ed Pipe
Cannot Convert 'Const Char*' to 'Lpcwstr {Aka Const Wchar_T*}'