Read file in binary and output in hex std::cin to std::cout
I was trying to achieve this using this code:
char c;
w开发者_JS百科hile (std::cin >> c)
std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<uint32_t>(c);
but it looks like it gets messed up reading in nulls (all nulls are removed from my file). How can I fix this?
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))
...
精彩评论