#pragma once #include #include #include #include #include #include namespace Incart::Common { class ByteArrayProvider { public: static uint32_t swapDWord(uint32_t value_) { return makeDWord(highWord(value_), lowWord(value_)); } static uint16_t swapWord(uint16_t value_) { return toWord(highByte(value_), lowByte(value_)); } static uint32_t makeDWord(uint16_t lowByte_, uint16_t highByte_) { return static_cast(lowByte_) + (static_cast(highByte_) << 16); } static uint16_t lowWord(uint32_t value_) { return static_cast(value_ & 0xFFFF); } static uint16_t highWord(uint32_t value_) { return static_cast(value_ >> 16); } static uint16_t getWordFromArray(const std::vector& _data, size_t _offset) { return static_cast(_data[_offset] + (static_cast(_data[_offset + 1]) << 8)); } static uint16_t toWord(uint8_t _lowByte, uint8_t _highByte) { return static_cast(_lowByte) + (static_cast(_highByte) << 0x8); } static uint8_t lowByte(uint16_t _value) { return static_cast(_value & 0xFF); } static uint8_t highByte(uint16_t _value) { return static_cast(_value >> 8); } static uint64_t ulongFromArray(const std::vector& data, int offset) { return ulongFromArray(&data[0], data.size(), offset); } static uint64_t ulongFromArray(const uint8_t* data, size_t dataSize, int offset) { if (dataSize - offset < 8) { return 0; } uint64_t result = 0; for (int i = 0; i < 8; i++) { result += (static_cast(data[offset + i]) << 8 * i); } return result; } static int32_t intThreeBytesFromArray(const std::vector& data, int offset) { return intThreeBytesFromArray(&data[0], data.size(), offset); } static int32_t intThreeBytesFromArray(const uint8_t* _data, size_t _dataSize, int _offset) { if (_dataSize - _offset < 3) { return 0; } std::vector tempData(4); for (int i = 0; i < 3; i++) { tempData[i] = _data[_offset + i]; } if ((tempData[2] & 0x80) != 0) { tempData[3] = 0xFF; } uint32_t result = getDWordFromArray(&tempData[0], 4, 0); return static_cast(result); } static void setIntThreeBytesToArray(uint8_t* _data, int32_t _value, size_t _offset) { setDWordToArray(static_cast(_value), _data, _offset, 3); if (_value < 0) { _data[_offset+2] |= 0x80; } } static uint32_t getDWordFromArray(const uint8_t* _data, size_t _dataSize, size_t _offset) { if (_dataSize - _offset < 4) { return 0; } uint32_t result = 0; for (int i = 0; i < 4; i++) { result += (static_cast(_data[_offset + i]) << 8 * i); } return result; } static void addDWord(uint32_t value_, std::vector& buffer_, size_t length_ = 4) { for (size_t i = 0; i < length_; ++i) { buffer_.push_back(static_cast(value_ >> (8 * i))); } } static void setDWordToArray(uint32_t value_, uint8_t* array_, size_t offset_, size_t length_ = 4) { for (size_t i = 0; i < length_; ++i) { array_[offset_++] = static_cast(value_ >> (8 * i)); } } static void addWord(uint16_t value_, std::vector& buffer_) { for (size_t i = 0; i < 2; ++i) { buffer_.push_back(static_cast(value_ >> (8 * i))); } } static void setWordToArray(uint16_t value_, uint8_t* array_, size_t offset_) { for (size_t i = 0; i < 2; ++i) { array_[offset_++] = static_cast(value_ >> (8 * i)); } } static void addQWord(uint64_t _value, std::vector& buffer_) { for (size_t i = 0; i < 8; ++i) { buffer_.push_back(static_cast(_value >> (8 * i))); } } static void setQWordToArray(uint64_t _value, uint8_t* _array, size_t _offset) { for (size_t i = 0; i < 8; ++i) { _array[_offset++] = static_cast(_value >> (8 * i)); } } static bool tryParseFormatFF(const std::string& text, std::vector& data) { char halfByte1; char halfByte2; char byteValue; if (text.length() % 2 != 0) return false; for (size_t i = 0; i < text.length(); i+=2) { if (!convertToByte(text[i], &halfByte1)) { return false; } if (!convertToByte(text[i+1], &halfByte2)) { return false; } byteValue = (halfByte1 << 4) + halfByte2; data.push_back(byteValue); } return true; } static bool convertToByte(char inputValue, char* byteValue) { if ((inputValue >= '0') && (inputValue <= '9')) { *byteValue = ((int)inputValue) - '0'; return true; } else if ((inputValue >= 'a') && (inputValue <= 'f')) { *byteValue = ((int)inputValue) - 'a' + 0x0A; return true; } else if ((inputValue >= 'A') && (inputValue <= 'F')) { *byteValue = ((int)inputValue) - 'A' + 0x0A; return true; } return false; } static void print(const QByteArray& bytes) { std::cout << "[" << std::hex; for (int i = 0; i < bytes.size(); i++) { std::cout << " " << std::setfill(' ') << std::setw(2) << static_cast(bytes[i] & 0xFF); } std::cout << "]" << std::dec; } static std::string packToText(const std::vector& byteArray) { std::string text = ""; for (size_t i = 0; i < byteArray.size(); i++) { if (i > 0) { text += ", "; } text += packByteToText(byteArray[i]); } return text; } static std::string packByteToText(char byteValue) { return n2hexstr(static_cast(byteValue & 0xFF), 2); } template static std::string n2hexstr(T w, size_t hex_len = sizeof(T) << 1) { static constexpr const char* digits = "0123456789ABCDEF"; std::string result(hex_len, '0'); for (size_t i = 0, j = (hex_len - 1) * 4; i < hex_len; ++i, j -= 4) { result[i] = digits[(w >> j) & 0x0f]; } return result; } static uint8_t toHexFromDec(uint8_t value_) { uint8_t lowByte = static_cast(value_ % 10); uint8_t highByte = static_cast(value_ / 10); return static_cast(lowByte + (highByte << 4)); } static bool toDecFromHex(uint8_t value_, uint8_t& decValue_) { uint8_t lowByte = static_cast(value_ & 0x0F); uint8_t highByte = static_cast(value_ >> 4); if ((highByte > 9) || (lowByte > 9)) { return false; } decValue_ = static_cast(lowByte + highByte * 10); return true; } }; } // namespace Incart::Common