c++ 字符数组-print and 写入文件

本文介绍了两种在C++中打印unsigned char类型数据的方法:一种是通过强制类型转换为int并使用std::cout输出;另一种是使用printf函数进行格式化打印。此外,还展示了如何将数据写入文件,包括打开文件、写入数据和关闭文件的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.print
 char buffer[22][8] = { 0 };
method1:
为了打印出unsigned char数据所对应的数值,可以将其强制转换成int类型,并做打印输出。
std::cout << "buffer[22][0]: " << std::hex << (int)buffer[22][0] << std::endl;
 
method2:
printf("buffer[0]: --- %x --- %d\n", (unsigned char)buffer[i][0], i+1);

2. write into file..

//#include <fstream>
std::ofstream in;//buffer data to be writen...
std::ofstream out;//can data...

in.open("in.txt", ios::trunc);

out.open("out.txt", ios::trunc);

for (int k = 0; k < 8; k++)
{
out << std::hex << (int)frame.data[k] << "\t";
}
out << "\n";

//close file.
in.close();
out.close();

RE:

1.https://blog.youkuaiyun.com/u012273127/article/details/53260156;

END

转载于:https://www.cnblogs.com/happyamyhope/p/9070635.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值