将结构数据类型转换为十六进制字符串,反之亦然

大多数 日志文件 在系统中生成的 二进制(0,1) 十六进制(0x) 格式。有时,您可能需要将这些数据映射为可读的格式。

null

将这个十六进制信息转换成系统定义的数据类型,例如“int/string/float”,相对来说比较容易。另一方面,当你有一些用户定义的数据类型,比如“struct”,这个过程可能会很复杂。

以下基本程序将帮助您完成上述操作。在现实世界中实现时,您需要更多的错误处理。

// C++ Program to convert a 'struct' in 'hex string'
// and vice versa
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
using namespace std;
struct Student_data
{
int student_id;
char name[16];
};
void convert_to_hex_string(ostringstream &op,
const unsigned char * data, int size)
{
// Format flags
ostream::fmtflags old_flags = op.flags();
// Fill characters
char old_fill  = op.fill();
op << hex << setfill( '0' );
for ( int i = 0; i < size; i++)
{
// Give space between two hex values
if (i>0)
op << ' ' ;
// force output to use hex version of ascii code
op << "0x" << setw(2) << static_cast < int >(data[i]);
}
op.flags(old_flags);
op.fill(old_fill);
}
void convert_to_struct(istream& ip, unsigned char * data,
int size)
{
// Get the line we want to process
string line;
getline(ip, line);
istringstream ip_convert(line);
ip_convert >> hex;
// Read in unsigned ints, as wrote out hex version
// of ascii code
unsigned int u = 0;
int i = 0;
while ((ip_convert >> u) && (i < size))
if ((0x00 <= u) && (0xff >= u))
data[i++] = static_cast <unsigned char >(u);
}
// Driver code
int main()
{
Student_data student1 = {1, "Rohit" };
ostringstream op;
// Function call to convert 'struct' into 'hex string'
convert_to_hex_string(op,
reinterpret_cast < const unsigned char *>(&student1),
sizeof (Student_data));
string output = op.str();
cout << "After conversion from struct to hex string:"
<< output << endl;
// Get te hex string
istringstream ip(output);
Student_data student2 = {0};
// Function call to convert 'hex string' to 'struct'
convert_to_struct(ip,
reinterpret_cast <unsigned char *>(&student2),
sizeof (Student_data));
cout << "After Conversion form hex to struct: " ;
cout << "Id : " << student2.student_id << endl;
cout << "Name : " << student2.name << endl;
return 0;
}


输出:

After conversion from struct to hex string:
0x01 0x00 0x00 0x00 0x52 0x6f 0x68 0x69 0x74 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00

After Conversion form hex to struct: 
Id 	: 1
Name 	: Rohit

本文由 罗希特·卡斯尔 .如果你喜欢GeekSforgek,并且想贡献自己的力量,你也可以写一篇文章,并将文章邮寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享