16 hex digits will fit in a uint64_t. uint64_t is unsigned long long which is an unsigned 64 bit integer. sizeof(uint64_t) is 8 bytes.
I do not know enough of JSON format such as how big a number it can store.
"myArray": [123456789012345, 2345678890123456]
In theory this is an array of big numbers without decimal points (integers) so it "should" convert it to a C array of uint64_t. There may be limitations on ArduinoJson on how big an integer it can handle. It may fail for integer values bigger than what can fit in a uint32_t.
"myArray": ["1234567890abcdef", "23456788901abcdef"]
This is an array of string holding hex digits. If ArduinoJson converts this to a C array of pointers to string constants, convert to array of uint64_t.
// Allocate on heap an array of uint64_t
myuint64Array = (uint64_t *)malloc(sizeof(uint64_t) * <number of elements in myArray>
for each index i in myArray
myuint64Array[i] = strtoull(myArray[i], NULL, 16);
strtoull() converts a string of digits (the 3rd parameter means base 16) to
unsigned long long integer.
I am very curious to see if ESP32 supports uint64_t and bsearch so I gave it a
try. Looks good in a small example.
int MyArraySize;
uint64_t *MyUint64Array;
void setup() {
Serial.begin(115200);
const char *myArray[] = {
"abcdef", "1234567890abcdef", "2345678901abcdef"
};
MyArraySize = sizeof(myArray) / sizeof(const char *);
MyUint64Array = (uint64_t *)malloc(sizeof(uint64_t) * MyArraySize);
for (int i = 0; i < MyArraySize; i++) {
MyUint64Array[i] = strtoull(myArray[i], NULL, 16);
// Show the string version and the converted uint64 version
Serial.print(myArray[i]);
Serial.print(" == ");
Serial.print(MyUint64Array[i], HEX);
Serial.println('?');
}
}
int uint64_compare(const void *a, const void *b)
{
// Do not use substraction as seen in some tutorials since these are unsigned
if (*(uint64_t*)a > *(uint64_t*)b) return 1;
if (*(uint64_t*)a < *(uint64_t*)b) return -1;
// If not > and not <, must be equal
return 0;
}
void loop() {
uint64_t searchkey;
uint64_t *resultkey;
// Search for every possible value. Sanity check: If "Not Found" is returned there is a bug!
for (int i = 0; i < MyArraySize; i++) {
searchkey = MyUint64Array[i];
resultkey = (uint64_t *)bsearch(&searchkey, MyUint64Array, MyArraySize, sizeof(uint64_t), uint64_compare);
Serial.print("Search for ");
Serial.print(searchkey, HEX);
Serial.print(" bsearch returns: ");
if (resultkey == NULL)
Serial.println("Not found");
else
Serial.println(*resultkey, HEX);
}
// Value not in the array so "Not Found" is the expected result.
searchkey = 0x1234ULL;
resultkey = (uint64_t *)bsearch(&searchkey, MyUint64Array, MyArraySize, sizeof(uint64_t), uint64_compare);
Serial.print("Search for ");
Serial.print(searchkey, HEX);
Serial.print(" bsearch returns: ");
if (resultkey == NULL)
Serial.println("Not found");
else
Serial.println(*resultkey, HEX);
delay(1000);
}