简体   繁体   中英

Read file in binary and output in hex std::cin to std::cout

I was trying to achieve this using this code:

char c;
while (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))
  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM