C++中cout输出十六进制数 二进制 dump

对于char型,它所表示的范围为-128~+127,假设有如下语句:

char data[3] = {0xab, 0xcd, 0xef}; 

初始化之后想打印出来,cout << data[0] << data[1] << data[2]; 发现都是乱码,没有按十六进制输出。

在ASCII中,一共定义了128个字符,其中33个无法显示,为0~31和127,32到126为可显示字符,当使用cout输出一个char型字符时,如果是可显示范围内,则输出相应可显示字符,其余的都输出乱码。

如果我们要使字符按十六进制输出,可以使用hex,但是发现cout << hex << data[0];没有输出十六进制,因为hex只对整数起作用,将data[0]转换为int,cout << hex << int(data[0]); 发现输出的结果前面带了很多f。因为data[0]是有符号数,最高位为1时,转换为int时,其余位都为1,cout << hex << (unsigned int) (unsigned char)data[0];结果正确。

对于小于16的char,输出只显示一位,如果希望都显示两位,可以在输出前设置cout << setfill('0') << setw(2);

复制代码
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    std::cout << "Hello World!\n";
    char n = 222;
    cout << hex << setfill('0') << setw(2) << (unsigned int)(unsigned char)(n)<<endl;
    cout << hex << setfill('0') << setw(2) << (int)n;
}
复制代码

 

输出到文件:

复制代码
#include <iostream>

#include <cstdio>#include <fstream>
 
using namespace std;
 ofstream file;

    file.open("commands_.log",
              std::ios_base::trunc );

    auto data = playback->serialize();
    //file.write(data->data(), data->size());

  {
       
      int len = data->size();
      const char* c = static_cast<const char*>(data->data());

      for (int i = 0; i < len;) {
        file << ",0x" << hex << setfill('0') << setw(2)
             << (unsigned int)(unsigned char)c[i];
        i++;
        if (i % 16 == 0)
          file << endl;
      }
    } 


  }
复制代码

 

也可以用 std::stoi()

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <iomanip>
int main() {
std::string line;
std::vector<std::string> hex_array;
std::ifstream file("e:/skp.txt");
//read string in format:" FF F0 EA 33 "
while (std::getline(file, line)) {
std::istringstream iss(line);
std::string hex_str;
while (iss >> hex_str) {
//std::cout <<hex_str<< std::endl;
hex_array.push_back(hex_str);
}
}
//output string as char byte: sucha as 65 to a.
std::cout << "total size:" << hex_array.size() << std::endl;
//print to readable charactor
for (auto byte_str : hex_array) {
char byte = static_cast<char>(std::stoi(byte_str, nullptr, 16));
std::cout << byte;
//std::cout << "0x" << std::hex << byte_str << ", ";
}
std::cout << std::endl;
//same as above. just output to a file as bin dump:
std::ofstream output_file("e:/output.txt");
for (auto byte_str : hex_array) {
char byte = static_cast<char>(std::stoi(byte_str, nullptr, 16));
output_file << byte;
//std::cout << "0x" << std::hex << byte_str << ", ";
}
output_file << std::endl;
//output value as hex format:
for (auto byte_str : hex_array) {
char byte = static_cast<char>(std::stoi(byte_str, nullptr, 16));
auto hex_value = std::stoi(byte_str, nullptr, 16);
std::cout << "0x" << std::hex << std::setfill('0') << std::setw(2) << hex_value << ", ";
}
std::cout << std::endl;
return 0;
}

 

posted @   Bigben  阅读(2608)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2018-05-31 分布式之消息队列复习精讲
2018-05-31 为什么分布式一定要有redis?
2018-05-31 我野在布达佩斯
2018-05-31 Spring Boot 1.X和2.X优雅重启实战
2013-05-31 nfc开源应用
点击右上角即可分享
微信分享提示