stuff-from-scratch/src/core/ByteUtils.cpp

164 lines
3.2 KiB
C++
Raw Normal View History

#include "ByteUtils.h"
bool ByteUtils::MostSignificantBitIsOne(char c)
{
return c & (1 << 7);
}
ByteUtils::Word ByteUtils::GetWordFirstBit(const Word word)
{
return word & ByteUtils::WORD_FIRST_BIT;
};
ByteUtils::Word ByteUtils::GetWordLastByte(const Word word)
{
return word & ByteUtils::WORD_LAST_BYTE;
}
unsigned char ByteUtils::getHigherNBits(unsigned char input, unsigned num)
{
return input >> 8 - num;
}
unsigned char ByteUtils::getLowerNBits(unsigned char input, unsigned num)
{
switch (num)
{
case 1:
return input & 0x01;
case 2:
return input & 0x03;
case 3:
return input & 0x07;
case 4:
return input & 0x0F;
case 5:
return input & 0x1F;
case 6:
return input & 0x3F;
case 7:
return input & 0x7F;
case 8:
return input;
default:
return 0;
}
}
unsigned char ByteUtils::getTwoBitsAtN(unsigned char input, unsigned n)
{
return (input & (0x03 << n)) >> n;
}
unsigned char ByteUtils::getMBitsAtN(unsigned char input, unsigned m, unsigned n)
{
switch (m)
{
case 1:
return (input & (0x01 << n)) >> n;
case 2:
return (input & (0x03 << n)) >> n;
case 3:
return (input & (0x07 << n)) >> n;
case 4:
return (input & (0x0F << n)) >> n;
case 5:
return (input & (0x1F << n)) >> n;
case 6:
return (input & (0x3F << n)) >> n;
case 7:
return (input & (0x7F << n)) >> n;
case 8:
return input;
default:
return 0;
}
}
unsigned char ByteUtils::getBitN(unsigned char input, unsigned n)
{
return input & (1 << n);
}
unsigned char ByteUtils::getFromString(const std::string& string)
{
unsigned char ret{0};
if (string.length() < 8)
{
return ret;
}
for(unsigned idx=0; idx<8; idx++)
{
if (string[idx] == '1')
{
ret |= (0x01 << (7 - idx));
}
}
return ret;
}
std::string ByteUtils::toString(unsigned char c)
{
std::string ret;
for(unsigned idx=0; idx<8; idx++)
{
ret += getBitN(c, 7 - idx) ? '1' : '0';
}
return ret;
}
void ByteUtils::ReverseBuffer(char* buffer, char* reverse, unsigned size, unsigned targetSize)
{
for(unsigned idx=0; idx<targetSize; idx++)
{
if (idx < size)
{
reverse[idx] = buffer[size - 1 -idx];
}
else
{
reverse[idx] = 0;
}
}
}
ByteUtils::Word ByteUtils::ToWord(char* buffer, bool reverse)
{
return ToType<Word>(buffer, reverse);
}
ByteUtils::DWord ByteUtils::ToDWord(char* buffer, bool reverse)
{
return ToType<DWord>(buffer, reverse);
}
ByteUtils::QWord ByteUtils::ToQWord(char* buffer, bool reverse)
{
return ToType<QWord>(buffer, reverse);
}
bool ByteUtils::Compare(char* buffer, const char* tag, unsigned size)
{
for(unsigned idx=0; idx<size; idx++)
{
if(tag[idx] != buffer[idx])
{
return false;
}
}
return true;
}
bool ByteUtils::CompareDWords(char* buffer, const char* tag)
{
return Compare(buffer, tag, sizeof(DWord));
}
bool ByteUtils::CompareWords(char* buffer, const char* tag)
{
return Compare(buffer, tag, sizeof(Word));
}