c++ - Obj-C NSLog a buffer -
i have got following function trying print datain out nslog,
some_function (const void *datain, size_t datainlength) { nsmutablestring *in1 = [nsmutablestring string]; (int i=0; i<datainlength; i++) [in1 appendformat:@"%02x", datain[i]]; }
this current code, upon compilation "error: subscript of pointer incomplete type 'const void'"
anyone know how can fix this?
you'll need couple of casts:
void some_function (const void *datain, size_t datainlength) { const char *cdata = (const char *)datain; nsmutablestring *in1 = [nsmutablestring string]; (int i=0; i<datainlength; i++) [in1 appendformat:@"%02x", (unsigned)cdata[i]]; }
the first [i]
knows how many bytes offset buffer (sizeof(char)
known) , secondly casting unsigned
that's printf-formatting expects size of %x
specifier values (you don't need one, i'm fussy).
Comments
Post a Comment