C++ Hex Parsing

前端 未结 4 489
醉话见心
醉话见心 2021-02-05 23:53

I\'m wondering how to convert a hex string to a human readable string (if that makes any sense) this would be my first real encounter with hex values so I\'m still learning abou

4条回答
  •  我寻月下人不归
    2021-02-06 00:25

    Hex is a way of displaying binary data. It is not "raw data" as you say. If the raw data you have contains a string, you should be able to see the string (possibly among other garbage) when you output it to the screen.

    Here's a loop to print the ASCII characters in a block of data. To get anything else, you will have to deal with its format.

    char *binary_data[ BUFFER_SIZE ];
    size_t len = BUFFER_SIZE;
    len = get_a_packet( data, len ); // or however you get data
    
    for ( char *text_ptr = binary_data; text_ptr != binary_data + len; ++ text_ptr ) {
        if ( * text_ptr <= '~' && * text_ptr >= ' ' ) { // if it's ascii
            cerr << * text_ptr; // print it out
        }
    }
    
    cerr << endl;
    

提交回复
热议问题