29constexpr int32_t BYTE_LENGTH =
static_cast<int32_t
>(
sizeof(int8_t) * 8);
31const uint8_t RANG_MASK_1_L[] = {0x01, 0x03, 0x07, 0x0F,
32 0x1F, 0x3F, 0x7F, 0xFF};
33const uint8_t RANG_MASK_0_L[] = {0xFE, 0XFC, 0xF8, 0xF0,
34 0xE0, 0xC0, 0x80, 0x00};
38Byte::Byte(
const uint8_t *value) : value_(const_cast<uint8_t *>(value)) {}
43 static const char HEX[] =
"0123456789ABCDEF";
44 uint8_t high =
static_cast<uint8_t
>(value >> 4);
45 uint8_t low =
static_cast<uint8_t
>(value & 0x0F);
46 return {HEX[high], HEX[low]};
52 std::string result =
"";
54 high =
static_cast<uint8_t
>((value >> 24) & 0xFF);
55 low =
static_cast<uint8_t
>((value >> 16) & 0xFF);
59 high =
static_cast<uint8_t
>((value >> 8) & 0xFF);
60 low =
static_cast<uint8_t
>(value & 0xFF);
67 return std::bitset<8 * sizeof(uint8_t)>(value).to_string();
71 static const uint8_t BIT_MASK_1[] = {0x01, 0x02, 0x04, 0x08,
72 0x10, 0x20, 0x40, 0x80};
73 if (pos >= 0 && pos < BYTE_LENGTH) {
74 *value_ |= BIT_MASK_1[pos];
79 static const uint8_t BIT_MASK_0[] = {0xFE, 0xFD, 0xFB, 0xF7,
80 0xEF, 0xDF, 0xBF, 0x7F};
81 if (pos >= 0 && pos < BYTE_LENGTH) {
82 *value_ &= BIT_MASK_0[pos];
87 return pos >= 0 && pos < BYTE_LENGTH && ((*value_ >> pos) % 2 == 1);
91 if (value_ !=
nullptr) {
103 const int32_t length) {
104 if (start_pos > BYTE_LENGTH - 1 || start_pos < 0 || length < 1) {
107 int32_t end_pos = std::min(start_pos + length - 1, BYTE_LENGTH - 1);
108 int32_t real_len = end_pos + 1 - start_pos;
109 uint8_t current_value_low = 0x00;
111 current_value_low = *value_ & RANG_MASK_1_L[start_pos - 1];
113 uint8_t current_value_high = *value_ & RANG_MASK_0_L[end_pos];
114 uint8_t middle_value = value & RANG_MASK_1_L[real_len - 1];
115 middle_value =
static_cast<uint8_t
>(middle_value << start_pos);
116 *value_ =
static_cast<uint8_t
>(current_value_high + middle_value +
127 if (start_pos > BYTE_LENGTH - 1 || start_pos < 0 || length < 1) {
130 int32_t end_pos = std::min(start_pos + length - 1, BYTE_LENGTH - 1);
131 int32_t real_len = end_pos + 1 - start_pos;
132 uint8_t result =
static_cast<uint8_t
>(*value_ >> start_pos);
133 result &= RANG_MASK_1_L[real_len - 1];
The class of one byte, which is 8 bits.
void set_bit_1(const int32_t pos)
Set the bit on a specified position to one.
static std::string byte_to_binary(const uint8_t value)
Transform an integer with the size of one byte to its binary represented by a string.
uint8_t get_byte_low_4_bits() const
Get a one-byte unsigned integer representing the lower 4 bits.
std::string to_hex_string() const
Transform to its hexadecimal represented by a string.
void set_value_low_4_bits(const uint8_t value)
Reset the lower 4 bits as the lower 4 bits of a specified one-byte unsigned integer.
bool is_bit_1(const int32_t pos) const
Check if the bit on a specified position is one.
void set_bit_0(const int32_t pos)
Set the bit on a specified position to zero.
uint8_t get_byte_high_4_bits() const
Get a one-byte unsigned integer representing the higher 4 bits.
void set_value_high_4_bits(const uint8_t value)
Reset the higher 4 bits as the higher 4 bits of a specified one-byte unsigned integer.
std::string to_binary_string() const
Transform to its binary represented by a string.
void set_value(const uint8_t value)
Reset this Byte by a specified one-byte unsigned integer.
Byte(const uint8_t *value)
Constructor which takes a pointer to a one-byte unsigned integer.
static std::string byte_to_hex(const uint8_t value)
Transform an integer with the size of one byte to its hexadecimal represented by a string.
uint8_t get_byte() const
Get the one-byte unsigned integer.