diff --git a/common/checksum/ByteArray.cpp b/common/checksum/ByteArray.cpp index 69562f7e2191ae3fd130fd435c21e6174689af19..2f7aa67f37663ceb3a8b5c3776e07e2b7681cdff 100644 --- a/common/checksum/ByteArray.cpp +++ b/common/checksum/ByteArray.cpp @@ -19,6 +19,7 @@ #include "common/checksum/ByteArray.hpp" #include <ostream> +#include <strings.h> //------------------------------------------------------------------------------ // constructor @@ -51,7 +52,21 @@ cta::ByteArray::ByteArray(const std::string &bytes) { } //------------------------------------------------------------------------------ -// constructor +// copy constructor +//------------------------------------------------------------------------------ +cta::ByteArray::ByteArray(const uint32_t value) { + m_size = sizeof value; + m_bytes = new uint8_t[sizeof value]; + bzero(m_bytes, sizeof m_bytes); + + m_bytes[0] = value & 0xFF; + m_bytes[1] = value >> 8 & 0xFF; + m_bytes[2] = value >> 16 & 0xFF; + m_bytes[3] = value >> 24 & 0xFF; +} + +//------------------------------------------------------------------------------ +// copy constructor //------------------------------------------------------------------------------ cta::ByteArray::ByteArray(const ByteArray &other) { m_size = other.m_size; diff --git a/common/checksum/ByteArray.hpp b/common/checksum/ByteArray.hpp index c297323a3c81dae7fb78d618e5171f139c3f8804..d4600543b5e6339ae12909716ca7555e8585614c 100644 --- a/common/checksum/ByteArray.hpp +++ b/common/checksum/ByteArray.hpp @@ -69,6 +69,15 @@ public: */ ByteArray(const std::string &bytes); + /** + * Constructor. + * + * Copies the specified uint32_t into this object in little endian byte order. + * + * @param value The value. + */ + explicit ByteArray(const uint32_t value); + /** * Copy constructor. */ diff --git a/common/checksum/ByteArrayTest.cpp b/common/checksum/ByteArrayTest.cpp index c84949be5813c60b3d96bd9503225826731741a3..4658e1ce1c26bbc80f16da88e68f181e60497c4d 100644 --- a/common/checksum/ByteArrayTest.cpp +++ b/common/checksum/ByteArrayTest.cpp @@ -81,6 +81,20 @@ TEST_F(cta_ByteArrayTest, string_constructor) { ASSERT_EQ((uint8_t)'o', byteArray.getBytes()[4]); } +TEST_F(cta_ByteArrayTest, uint32_t_constructor) { + using namespace cta; + + const uint32_t value = 0x10203040; + const ByteArray byteArray(value); + + // uint32_t should be stored in little endian byte order + ASSERT_EQ((uint32_t)4, byteArray.getSize()); + ASSERT_EQ((uint8_t)0x40, byteArray.getBytes()[0]); + ASSERT_EQ((uint8_t)0x30, byteArray.getBytes()[1]); + ASSERT_EQ((uint8_t)0x20, byteArray.getBytes()[2]); + ASSERT_EQ((uint8_t)0x10, byteArray.getBytes()[3]); +} + TEST_F(cta_ByteArrayTest, copy_constructor) { using namespace cta;