1

I have a buffer which is filled by an external hardware (DMA). When this happens, I can print the data using the following code:

void* buffer;
...
/*buffer is initialized and filled at some point*/
...
for (k=0; k<10; k++) {
    printf("%d ", ((unsigned int*) buffer)[k]);
}

I would like to put this data into JSON format using the functions provided by libjson. This is my attempt:

json_object * jobj = json_object_new_object();

json_object *jarray = json_object_new_array();

if (!jobj || !jarray) {
    return NULL;
}
...
for (k=0; k<10; k++) {
    printf("%d ", (((unsigned int*) buffer);
    sprintf(str, "%d", ((unsigned int*) buffer)[k]);
    json_object *jstring = json_object_new_string(str);
    json_object_array_add(jarray,jstring);
}
json_object_object_add(jobj,"array", jarray);

This code works but I'm not sure if so many definitions of "json_object *jstring" may be correct, especially for large amounts of data.

wuampa
  • 273
  • 4
  • 15
  • What errors? Can you try being more specific? Also you didn't add the language you're using in the tags. – Phiter Mar 14 '18 at 11:37
  • Sorry, I have edited the thread to provide more information – wuampa Mar 14 '18 at 11:45
  • Your printing code (`printf("%d ", (((unsigned int*) buffer);`) makes no sense at all. It prints the pointer itself, cast to a pointer, as a signed `int`, which is instant undefined behavior. – unwind Mar 14 '18 at 11:57
  • "printf("%d ", (((unsigned int*) buffer);" won't do what you think it does. – Bjorn A. Mar 14 '18 at 12:01
  • I think I took this code from: "https://stackoverflow.com/questions/15292237/printing-a-void-variable-in-c". The values I'm printing seem correct. – wuampa Mar 14 '18 at 12:02
  • Sorry, editing the code I had erased [k]. – wuampa Mar 14 '18 at 12:05

0 Answers0