laforge
/
openbts-osmo
Archived
1
0
Fork 0

common libs: Implemented BitVector::unhex() which reads a hex string into a BitVector.

(cherry picked from commit d017dd20de029dbaa275335450728885f19ed01d)
This commit is contained in:
Alexander Chemeris 2010-11-04 18:34:34 +03:00
parent e2e267dcca
commit a2ce7ae628
2 changed files with 27 additions and 0 deletions

View File

@ -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

View File

@ -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*);
};