44 const int byteOffset = bitOffset / 8;
45 int lBitOffset = bitOffset % 8;
47 if (byteOffset >=
static_cast<int>((src.size() - (lBitOffset ? 1 : 0)))) {
51 std::vector<uint8_t> dest(nbBytes);
53 if (lBitOffset == 0) {
56 const int rightShift = 8 - bitOffset;
57 for (
int i = 0, j = byteOffset; j < byteOffset + nbBytes; i++, j++) {
58 dest[i] = ((src[j] << lBitOffset) | ((src[j + 1] & 0xFF) >> rightShift));
71 std::vector<uint8_t> data(nbBytes);
76 data[i] = ((src >> shift) & 0xFF);
86 if (offset < 0 || offset >
static_cast<int>(src.size() - 2)) {
90 return ((src[offset] & 0xFF) << 8) | (src[offset + 1] & 0xFF);
103 if ((offset + nbBytes) >
static_cast<int>(src.size())) {
115 uint32_t complement = 0xFFFFFFFF;
116 int lOffset = offset;
117 int lNbBytes = nbBytes;
118 bool negative =
false;
121 negative = src[lOffset] > 0x7F;
124 while (lNbBytes > 0) {
126 val |= ((src[lOffset++] & 0xFF) << (8 * (--lNbBytes)));
127 complement &= ~(0xFF << 8 * lNbBytes);
131 if (isSigned && negative) {
136 return (uint32_t)val;
144 int lOffset = offset;
145 int lNbBytes = nbBytes;
148 if (lOffset < 0 || lOffset >
static_cast<int>(src.size() - lNbBytes)) {
153 uint64_t complement = 0xFFFFFFFFFFFFFFFF;
154 bool negative =
false;
157 while (lNbBytes > 0) {
159 negative = negative ? true : ((src[lOffset] & 0xFF) > 0x7F ?
true :
false);
160 val |= (((uint64_t)(src[lOffset++] & 0xFF)) << (8 * (--lNbBytes)));
161 complement &= ~(((uint64_t)0xFF) << 8 * lNbBytes);
165 if (isSigned && negative) {
169 return (uint64_t)val;
173 std::vector<uint8_t>& dest,
177 if (offset < 0 || offset >
static_cast<int>(dest.size() - nbBytes)) {
191 if (hex.length() % 2 != 0) {
static Assert & getInstance()
Assert & notEmpty(const std::string &obj, const std::string &name)
Assert & isEqual(const size_t number, const size_t value, const std::string &name)
static bool isValidHexString(const std::string &hex)
static uint32_t extractInt(const std::vector< uint8_t > &src, const int offset, const int nbBytes, const bool isSigned)
static const std::string normalizeHexString(const std::string &hex)
static int threeBytesToInt(const std::vector< uint8_t > &bytes, const int offset)
static int fourBytesToInt(const std::vector< uint8_t > &bytes, const int offset)
static int twoBytesSignedToInt(const std::vector< uint8_t > &bytes, const int offset)
static const std::string toHex(const std::vector< uint8_t > &src)
static std::vector< uint8_t > fromHex(const std::string &hex)
static int threeBytesSignedToInt(const std::vector< uint8_t > &bytes, const int offset)
static uint16_t extractShort(const std::vector< uint8_t > &src, const int offset)
static int twoBytesToInt(const std::vector< uint8_t > &bytes, const int offset)
static const std::vector< uint8_t > extractBytes(const std::vector< uint8_t > &src, const int bitOffset, const int nbBytes)
static void copyBytes(const uint64_t src, std::vector< uint8_t > &dest, const int offset, const int nbBytes)
static uint64_t extractLong(const std::vector< uint8_t > &src, const int offset, const int nbBytes, const bool isSigned)
static const std::vector< uint8_t > toByteArray(const std::string &hex)
static const std::string toHex(const std::vector< uint8_t > &tab)
static bool isValid(const std::string &hex)
static void arraycopy(const std::vector< char > &src, size_t srcPos, std::vector< char > &dest, size_t destPos, size_t length)