common libs: Implemented BitVector::unhex() which reads a hex string into a BitVector.
(cherry picked from commit d017dd20de029dbaa275335450728885f19ed01d)
This commit is contained in:
parent
e2e267dcca
commit
a2ce7ae628
|
@ -27,6 +27,7 @@
|
|||
|
||||
#include "BitVector.h"
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
@ -576,4 +577,26 @@ void BitVector::hex(ostream& os) const
|
|||
os << std::dec;
|
||||
}
|
||||
|
||||
bool BitVector::unhex(const char* src)
|
||||
{
|
||||
// Assumes MSB-first packing.
|
||||
unsigned int val;
|
||||
unsigned digits = size()/4;
|
||||
for (unsigned i=0; i<digits; i++) {
|
||||
if (sscanf(src+i, "%1x", &val) < 1) {
|
||||
return false;
|
||||
}
|
||||
fillField(i*4,val,4);
|
||||
}
|
||||
unsigned whole = digits*4;
|
||||
unsigned rem = size() - whole;
|
||||
if (rem>0) {
|
||||
if (sscanf(src+digits, "%1x", &val) < 1) {
|
||||
return false;
|
||||
}
|
||||
fillField(whole,val,rem);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// vim: ts=4 sw=4
|
||||
|
|
|
@ -334,6 +334,10 @@ class BitVector : public Vector<char> {
|
|||
/** Make a hexdump string. */
|
||||
void hex(std::ostream&) const;
|
||||
|
||||
/** Unpack from a hexdump string.
|
||||
* @returns true on success, false on error. */
|
||||
bool unhex(const char*);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
|
Reference in New Issue