如何将ASCII字符的十六进制值写入文本文件?

编程入门 行业动态 更新时间:2024-10-27 12:29:44
本文介绍了如何将ASCII字符的十六进制值写入文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

void WriteHexToFile(std :: ofstream& stream,void * ptr ,int buflen,char * prefix) { unsigned char * buf =(unsigned char *)ptr; (int i = 0; i 我试过做stream.hex,stream。 setf(std :: ios :: hex),以及在Google上搜索一下。我也试过:

stream<< stream.hex<< (int)buf [i]<< '';

但这似乎并不奏效。

下面是一个当前产生的输出示例:

ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ ÍÍÍÍ$ b b ÍÍÍÍ$ $ $ $ bÍÍ我希望输出看起来像下面这样:

FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00 FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00 FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00 FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00 FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00 FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00

解决方案

#include< iostream> 使用namespace std; int main(){ char c = 123; cout<<十六进制<< int(c)<< ENDL; }

编辑::零填充:

#include< iostream> #include< iomanip> 使用namespace std; int main(){ char c = 13; cout<<十六进制<< (2)<< setfill('0')<< int(c)<< ENDL; }

Here is what I currently have so far:

void WriteHexToFile( std::ofstream &stream, void *ptr, int buflen, char *prefix ) { unsigned char *buf = (unsigned char*)ptr; for( int i = 0; i < buflen; ++i ) { if( i % 16 == 0 ) { stream << prefix; } stream << buf[i] << ' '; } }

I've tried doing stream.hex, stream.setf( std::ios::hex ), as well as searching Google for a bit. I've also tried:

stream << stream.hex << (int)buf[i] << ' ';

But that doesn't seem to work either.

Here is an example of some output that it currently produces:

Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í

I would like the output to look like the following:

FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00 FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00 FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00 FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00 FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00 FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00

解决方案

#include <iostream> using namespace std; int main() { char c = 123; cout << hex << int(c) << endl; }

Edit: with zero padding:

#include <iostream> #include <iomanip> using namespace std; int main() { char c = 13; cout << hex << setw(2) << setfill('0') << int(c) << endl; }

更多推荐

如何将ASCII字符的十六进制值写入文本文件?

本文发布于:2023-10-27 18:33:08,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   文本文件   字符   ASCII   十六进制值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!