| Line |
Branch |
Exec |
Source |
| 1 |
|
|
#include "hex_str.h" |
| 2 |
|
|
|
| 3 |
|
|
#include <sstream> |
| 4 |
|
|
#include <iomanip> |
| 5 |
|
|
|
| 6 |
|
|
/// Turns bytes into a hexadecimal string |
| 7 |
|
|
/// \param data bytes that have to be converted |
| 8 |
|
|
/// \return hexadecimal string |
| 9 |
|
✗ |
std::string hex_str::bytes_to_hex_str (const std::vector<uint8_t>& data) |
| 10 |
|
|
{ |
| 11 |
|
✗ |
std::stringstream ss; |
| 12 |
|
✗ |
ss << std::hex; |
| 13 |
|
|
|
| 14 |
|
✗ |
for (unsigned char i : data) |
| 15 |
|
|
{ |
| 16 |
|
✗ |
ss << std::setw (2) << std::setfill ('0') << (int)i << " "; |
| 17 |
|
|
} |
| 18 |
|
|
|
| 19 |
|
✗ |
return ss.str (); |
| 20 |
|
|
} |
| 21 |
|
|
|
| 22 |
|
|
/// Turns a string into it's hexadecimal form |
| 23 |
|
|
/// \param payload_string string that has to be converted |
| 24 |
|
|
/// \return hexadecimal representation of payload_string |
| 25 |
|
✗ |
std::string hex_str::str_to_hex_str (const std::string& payload_string) |
| 26 |
|
|
{ |
| 27 |
|
✗ |
std::vector<uint8_t> byte_data; |
| 28 |
|
✗ |
byte_data.reserve (payload_string.length ()); |
| 29 |
|
✗ |
for (int i = 0; i < payload_string.length (); i++) |
| 30 |
|
|
{ |
| 31 |
|
✗ |
byte_data.push_back ((uint8_t)payload_string[i]); |
| 32 |
|
|
} |
| 33 |
|
|
|
| 34 |
|
✗ |
std::string payload_bytes_string = hex_str::bytes_to_hex_str (byte_data); |
| 35 |
|
✗ |
return payload_bytes_string; |
| 36 |
|
|
} |
| 37 |
|
|
|
| 38 |
|
✗ |
std::vector<uint8_t> hex_str::str_hex_to_bytes (const std::string& payload_string) |
| 39 |
|
|
{ |
| 40 |
|
✗ |
std::vector<uint8_t> bytes; |
| 41 |
|
|
|
| 42 |
|
✗ |
for (unsigned int i = 0; i < payload_string.length (); i += 2) |
| 43 |
|
|
{ |
| 44 |
|
✗ |
std::string byteString = payload_string.substr (i, 2); |
| 45 |
|
✗ |
char byte = (char)strtol (byteString.c_str (), NULL, 16); |
| 46 |
|
✗ |
bytes.push_back (byte); |
| 47 |
|
|
} |
| 48 |
|
|
|
| 49 |
|
✗ |
return bytes; |
| 50 |
|
|
} |
| 51 |
|
|
|