c - Send data from Host App to Chrome Extension -
after hours of work, i'm able run extension , host app when activate extension, everytime try receive data, undefined.
this code:
int main(int argc, char* argv[]) { char message[] = "{\"t\": \"t\"}"; unsigned int len = strlen(message); _setmode(_fileno(stdout), _o_binary); printf("%c%c%c%c", (char) 10, (char) 0, (char) 0, (char) 0); printf("%s", message); return 0; }
what doing wrong?
%c
takes int
argument (intrpreted unsigned char
), not char
. can/will result in wrong data being printed. strlen
returns size_t
. should read man-pages of functions use.
important: when passing binary values (not characters), should either use uint8_t
or - @ least unsigned char
. remember char
can signed or unsigned, depending on implementation. might lead surprises on arithmetic operations.
note:
printf("%c%c%c%c", (char) 10, (char) 0, (char) 0, (char) 0); printf("%s", message);
can combined (casts removed):
printf("%c%c%c%c%s", 10, 0, 0, 0, message);
if there no library support, should think writing functions serialize basic types like:
static void print_u32(uint32_t value) { ( size_t n = 0 ; n < 4 ; n-- ) { putc((unsigned char)value); // still passing int ! value >>= 8; } } ... print_string( ... print_u32(strlen(message)); print_string(message);
note _setmode
not standard library function. after googling, seems bsd extension supported windows (just in case want extension portable).
Comments
Post a Comment