Added ToHexString() and made ostream serialization use this

This commit is contained in:
Bart Trzynadlowski 2017-09-19 19:43:06 +00:00
parent e12a945ddd
commit 86631695d7
3 changed files with 83 additions and 10 deletions

View file

@ -208,6 +208,39 @@ namespace Util
return out;
}
std::string BitRegister::ToHexString() const
{
const char digits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
if (Empty())
return std::string("");
size_t partial = Size() & 3;
size_t num_digits = Size() / 4 + (partial != 0 ? 1 : 0);
std::string out(num_digits + 2, '0');
out[0] = '0';
out[1] = 'x';
size_t idx = 0;
size_t digit_idx = 2;
size_t digit = 0;
if (partial != 0)
{
for (idx = 0; idx < partial; idx++)
{
digit <<= 1;
digit |= m_bits[idx];
}
out[digit_idx++] = digits[digit];
}
while (idx < Size())
{
digit = m_bits[idx++] << 3;
digit |= m_bits[idx++] << 2;
digit |= m_bits[idx++] << 1;
digit |= m_bits[idx++] << 0;
out[digit_idx++] = digits[digit];
}
return out;
}
std::ostream &operator<<(std::ostream &os, const BitRegister &reg)
{
if (reg.Empty())
@ -215,14 +248,7 @@ namespace Util
os << "[ empty ]";
return os;
}
char *buf = new char[reg.Size() + 1];
buf[reg.Size()] = 0;
for (size_t i = 0; i < reg.Size(); i++)
{
buf[i] = reg.m_bits[i] == 0 ? '0' : '1';
}
os << "[ " << reg.Size() << ": " << buf << " ]";
delete [] buf;
os << "[ " << reg.Size() << ": " << reg.ToHexString() << " ]";
return os;
}
} // Util

View file

@ -41,11 +41,14 @@ namespace Util
void Set(const std::string &value);
void SetZeros(size_t count);
void SetOnes(size_t count);
void SetNoBitValue(uint8_t bit);
void Reset();
// Configuration state
void SetNoBitValue(uint8_t bit);
// String serialization
std::string ToBinaryString() const;
std::string ToHexString() const;
friend std::ostream &operator<<(std::ostream &os, const BitRegister &reg);
private:

View file

@ -8,7 +8,6 @@ int main(int argc, char **argv)
Util::BitRegister reg;
// Test
reg.SetZeros(1);
expected.push_back("0");
@ -210,6 +209,51 @@ int main(int argc, char **argv)
expected.push_back("00000000000000101001001000110011");
results.push_back(reg.ToBinaryString());
// Test hex printing
reg.SetOnes(1);
expected.push_back("0x1");
results.push_back(reg.ToHexString());
reg.SetZeros(1);
expected.push_back("0x0");
results.push_back(reg.ToHexString());
reg.SetZeros(2);
expected.push_back("0x0");
results.push_back(reg.ToHexString());
reg.SetZeros(3);
expected.push_back("0x0");
results.push_back(reg.ToHexString());
reg.SetZeros(4);
expected.push_back("0x0");
results.push_back(reg.ToHexString());
reg.SetZeros(5);
expected.push_back("0x00");
results.push_back(reg.ToHexString());
reg.Set("11001");
expected.push_back("0x19");
results.push_back(reg.ToHexString());
reg.Set("111001");
expected.push_back("0x39");
results.push_back(reg.ToHexString());
reg.Set("1111001");
expected.push_back("0x79");
results.push_back(reg.ToHexString());
reg.Set("01111001");
expected.push_back("0x79");
results.push_back(reg.ToHexString());
reg.Set("101111001");
expected.push_back("0x179");
results.push_back(reg.ToHexString());
// Check results
size_t num_failed = 0;
for (size_t i = 0; i < expected.size(); i++)