#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Function to create a dynamic byte array from a string
unsigned char* createByteArray(const char* str, size_t* len) {
*len = strlen(str);
unsigned char* byteArray = (unsigned char*)malloc(*len + 1); // +1 for the null terminator
if (byteArray == NULL) {
printf("Memory allocation failed\n");
return NULL;
}
memcpy(byteArray, str, *len + 1);
return byteArray;
}
int main() {
char* str = "C Programming";
size_t len = 0;
// Eventually, it's the same array...
unsigned char* byteArray = createByteArray(str, &len);
if (byteArray == NULL) {
return 1; // Exit the program if allocation failed
}
for (size_t i = 0; i < len + 1; i++) {
printf("%02x ", byteArray[i]);
}
printf("\n");
// Free the allocated memory
free(byteArray);
return 0;
}
/*
run:
43 20 50 72 6f 67 72 61 6d 6d 69 6e 67 00
*/