开发者

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

开发者 https://www.devze.com 2023-04-12 15:11 出处:网络
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<u

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))
  ...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号