Uniot Core
0.8.1
Loading...
Searching...
No Matches
Bytes Class Reference

#include <Bytes.h>

Public Types

using Filler = std::function<size_t(uint8_t *buf, size_t size)>
 Function type for filling byte arrays.
 

Public Member Functions

 Bytes ()
 Default constructor.
 
 Bytes (const uint8_t *data, size_t size)
 Constructor from raw byte array.
 
 Bytes (const char *str)
 Constructor from C-string.
 
template<typename T>
 Bytes (T value)
 Constructor from integral type.
 
 Bytes (const Bytes &value)
 Copy constructor.
 
 Bytes (const String &value)
 Constructor from Arduino String.
 
virtual ~Bytes ()
 Destructor.
 
Bytesoperator= (const Bytes &rhs)
 Assignment operator.
 
Bytesoperator= (const String &rhs)
 Assignment from Arduino String.
 
size_t fill (Filler filler)
 Fills the buffer using a provided filler function.
 
Bytesprune (size_t newSize)
 Reduces the size of the buffer.
 
const uint8_t * raw () const
 Gets a const pointer to the raw byte array.
 
Bytesterminate ()
 Ensures the byte array is null-terminated.
 
const char * c_str () const
 Gets the byte array as a C string.
 
String toString () const
 Converts the byte array to an Arduino String.
 
String toHexString () const
 Converts the byte array to a hexadecimal string.
 
size_t size () const
 Gets the size of the byte array.
 
void clean ()
 Deallocates the internal buffer.
 
uint32_t checksum () const
 Calculates a checksum of the byte array.
 

Static Public Member Functions

static Bytes fromHexString (const String &hexStr)
 Creates a Bytes object from a hexadecimal string.
 

Member Typedef Documentation

◆ Filler

using Bytes::Filler = std::function<size_t(uint8_t *buf, size_t size)>

Function type for filling byte arrays.

A callable that takes a buffer and size, fills the buffer, and returns the number of bytes filled.

Constructor & Destructor Documentation

◆ Bytes() [1/6]

Bytes::Bytes ( )
inline

Default constructor.

Creates an empty Bytes object with no allocated memory.

◆ Bytes() [2/6]

Bytes::Bytes ( const uint8_t * data,
size_t size )
inline

Constructor from raw byte array.

If data is nullptr but size is non-zero, space will be allocated but not initialized.

Parameters
dataPointer to byte array to copy, or nullptr to just allocate
sizeSize of the byte array

◆ Bytes() [3/6]

Bytes::Bytes ( const char * str)
inline

Constructor from C-string.

Creates a Bytes object containing the string plus its null terminator.

Parameters
strNull-terminated C string

◆ Bytes() [4/6]

template<typename T>
Bytes::Bytes ( T value)
inline

Constructor from integral type.

Parameters
valueIntegral value to convert to bytes

Creates a Bytes object with the binary representation of the integral value. Only works with integral types (int, char, long, etc.).

◆ Bytes() [5/6]

Bytes::Bytes ( const Bytes & value)
inline

Copy constructor.

Creates a deep copy of another Bytes object.

Parameters
valueThe Bytes object to copy

◆ Bytes() [6/6]

Bytes::Bytes ( const String & value)
inline

Constructor from Arduino String.

Creates a Bytes object from the contents of an Arduino String.

Parameters
valueArduino String to convert to bytes

◆ ~Bytes()

virtual Bytes::~Bytes ( )
inlinevirtual

Destructor.

Frees allocated memory.

Member Function Documentation

◆ c_str()

const char * Bytes::c_str ( ) const
inline

Gets the byte array as a C string.

Returns the buffer as a C string. The buffer should be null-terminated first.

Return values
char*Pointer to the internal buffer

◆ checksum()

uint32_t Bytes::checksum ( ) const
inline

Calculates a checksum of the byte array.

Uses CRC32 algorithm to generate a checksum of the data.

Return values
uint32_tChecksum of the byte array

◆ clean()

void Bytes::clean ( )
inline

Deallocates the internal buffer.

Frees all allocated memory and resets the object to empty state.

◆ fill()

size_t Bytes::fill ( Filler filler)
inline

Fills the buffer using a provided filler function.

Uses the provided function to fill the buffer with data. The filler should return the number of bytes actually written.

Parameters
fillerFunction to fill the buffer
Return values
size_tNumber of bytes filled
0If the filler function is null

◆ fromHexString()

static Bytes Bytes::fromHexString ( const String & hexStr)
inlinestatic

Creates a Bytes object from a hexadecimal string.

Converts a hexadecimal string to its binary representation. The hexadecimal string length must be even.

Parameters
hexStrString containing hexadecimal characters (e.g. "1A2B3C")
Return values
BytesObject with the binary data
BytesEmpty object if the string length is invalid

◆ operator=() [1/2]

Bytes & Bytes::operator= ( const Bytes & rhs)
inline

Assignment operator.

Creates a deep copy of the data from another Bytes object.

Parameters
rhsThe Bytes object to copy from
Return values
Bytes&Reference to this object

◆ operator=() [2/2]

Bytes & Bytes::operator= ( const String & rhs)
inline

Assignment from Arduino String.

Copies the contents of an Arduino String and adds a null terminator.

Parameters
rhsArduino String to copy from
Return values
Bytes&Reference to this object

◆ prune()

Bytes & Bytes::prune ( size_t newSize)
inline

Reduces the size of the buffer.

Reduces the size of the buffer. If newSize is larger than the current size, no action is taken.

Parameters
newSizeThe new size to shrink to
Return values
Bytes&Reference to this object

◆ raw()

const uint8_t * Bytes::raw ( ) const
inline

Gets a const pointer to the raw byte array.

Provides direct access to the internal buffer. Do not modify or free this pointer.

Return values
uint8_tPointer to this object

◆ size()

size_t Bytes::size ( ) const
inline

Gets the size of the byte array.

Return values
size_tSize of the byte array in bytes

◆ terminate()

Bytes & Bytes::terminate ( )
inline

Ensures the byte array is null-terminated.

Adds a null terminator if one isn't already present, useful for string operations.

Return values
Bytes&Reference to this object

◆ toHexString()

String Bytes::toHexString ( ) const
inline

Converts the byte array to a hexadecimal string.

Each byte is converted to a two-character hexadecimal representation.

Return values
StringString containing hexadecimal representation of the bytes

◆ toString()

String Bytes::toString ( ) const
inline

Converts the byte array to an Arduino String.

Assumes the bytes represent a null-terminated string.

Return values
StringString representation of the bytes as a character string

The documentation for this class was generated from the following file: