Converting UUIDs to Static uint8_t
Arrays: A Guide for Embedded Developers
The Problem:
Many embedded systems and communication protocols work with raw binary data, often represented as uint8_t
arrays. However, Universally Unique Identifiers (UUIDs) are commonly stored and exchanged as strings, making direct integration into binary environments challenging. This article explores how to convert UUID strings to static uint8_t
arrays, a crucial task for developers working with Bluetooth Low Energy (BLE) advertising data and similar applications.
Scenario and Original Code:
Imagine you're working on a BLE device that needs to advertise its unique ID. You have a UUID stored as a string:
char uuid_str[] = "0000180D-0000-1000-8000-00805F9B34FB";
Your goal is to convert this string into a static uint8_t
array adv_data
for use in BLE advertising packets.
The Solution:
Directly casting a string to a uint8_t
array will not work as expected. Instead, we need to manually parse the UUID string, extract each hexadecimal byte, and store it in the adv_data
array.
#include <string.h>
static uint8_t adv_data[31] = {0};
void convert_uuid_to_array(const char *uuid_str) {
// Check if the UUID string is valid (16 bytes + 4 hyphens + 1 null terminator).
if (strlen(uuid_str) != 36) {
return;
}
// Parse each hexadecimal byte from the UUID string.
for (int i = 0, j = 0; i < 36; i += 2, j++) {
// Skip hyphens.
if (uuid_str[i] == '-' || uuid_str[i+1] == '-') {
continue;
}
// Convert hexadecimal characters to a byte.
adv_data[j] = (uint8_t)strtol(&uuid_str[i], NULL, 16);
}
}
Explanation:
- Validation: The code first checks if the UUID string is a valid 36-character string, including hyphens and the null terminator.
- Parsing: The code then iterates through the string, extracting each two-character hexadecimal byte.
- Conversion: The
strtol()
function converts the hexadecimal string representation to an integer, which is then cast to auint8_t
and stored in theadv_data
array.
Additional Considerations:
- Endianness: Remember that the UUID is stored in big-endian order (most significant byte first). If your platform is little-endian, you might need to reverse the byte order in
adv_data
. - UUID Type: Some BLE applications require a specific UUID type, such as the 128-bit UUID (16 bytes). The code assumes a 128-bit UUID. If you're dealing with a different UUID type, adjust the code accordingly.
- BLE Advertising Data: The
adv_data
array is used for BLE advertising. Ensure that the format and content of this array comply with the Bluetooth specification.
Resources:
- Bluetooth Specification: https://www.bluetooth.com/specifications/specs/
- UUID Format: https://en.wikipedia.org/wiki/Universally_unique_identifier
- strtol() Function: https://www.cplusplus.com/reference/cstdlib/strtol/
Conclusion:
Converting a UUID string to a static uint8_t
array is essential for embedded systems that use UUIDs for identification. By understanding the principles of UUIDs and applying the provided code snippet, developers can seamlessly integrate these unique identifiers into their applications. Remember to adapt the code based on your specific requirements and the context of your embedded system.