Conversion of Struct data type to Hex String and vice versa

Most of the log files produced in system are either in binary(0,1) or hex(0x) formats. Sometimes you might need to map this data into readable format.

Conversion of this hex information into system defined data types such as ‘int/string/float’ is comparatively easy. On the other hand when you have some user defined data types such as ‘struct’ the process can be complicated.

Following basic program will help you with above mentioned operation. While implementing in real world you need more error handling.



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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:\n"
         << 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 << "\nAfter Conversion form hex to struct: \n";
    cout << "Id \t: " << student2.student_id << endl;
    cout << "Name \t: "<< student2.name << endl;
  
    return 0;
}

chevron_right


Output:

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

This article is contributed by Rohit Kasle. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.