1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
/*
* Dibbler - a portable DHCPv6
*
* authors: Tomasz Mrugalski <thomson@klub.com.pl>
*
* Released under GNU GPL v2 licence
*
*/
#include <vector>
#include <sstream>
#include <iomanip>
#include "hex.h"
#include "Logger.h"
std::vector<uint8_t> textToHex(std::string buf) {
// if it starts with 0x, just ignore that prefix
if ( (buf.length() >= 2) && (buf[0] == '0') && (buf[1] == 'x')) {
buf = buf.substr(2);
}
std::vector<uint8_t> tmp;
int textLen = buf.length();
unsigned char digit;
int i=0;
bool twonibbles = false;
char value = 0;
while (i < textLen)
{
if (buf[i]==':') {
i++;
}
digit = buf[i];
if (isalpha(digit))
digit = toupper(digit)-'A'+10;
else
digit -= '0';
value <<= 4;
value |= digit;
i++;
if (twonibbles) {
twonibbles = false;
tmp.push_back(value);
value = 0;
} else
twonibbles = true;
}
return tmp;
}
std::string hexToText(const uint8_t* buf, size_t buf_len, bool add_colons /*= false*/,
bool add_0x /* = false*/) {
std::ostringstream tmp;
if (add_0x)
tmp << "0x";
for(unsigned i = 0; i < buf_len; i++) {
if (i)
tmp << ":";
tmp << std::setfill('0') << std::setw(2) << std::hex
<< (unsigned short)((unsigned char) buf[i]);
}
return tmp.str();
}
std::string hexToText(const std::vector<uint8_t>& vector, bool add_colons /*= false*/,
bool add_0x /*= false*/) {
return hexToText(&vector[0], vector.size(), add_colons, add_0x);
}
|