Question

char* createMSG(uint8_t i,uint16_t port) {
char *buff; 
buff = (char*) calloc(1,6);
uint8_t id, tmp;
tmp = 0;
id = 2;
memcpy(buff, &id, sizeof(uint8_t));
memcpy(buff+1, &i, sizeof(uint8_t));
memcpy(buff+2, &port, sizeof(uint16_t));
memcpy(buff+2+2, &tmp, sizeof(uint16_t));
memcpy(buff+2+2+1, &tmp, sizeof(uint16_t));
printf("created SV_CON_REP: id: %d accept: %d port %d\n",*buff,*(buff+1),*    (buff+2));    return buff;
}

I need to copy the port in an uint32_t. It prints that port is Null.

EDIT Function call: char* tmp; uint8_t i; i = 9; uint16_t port; port = 1234; tmp = createMSG(i,port);

Output: created MSG: id: 2 accept: 0 port 0

Was it helpful?

Solution

I was copy this function, but under windows.

uint8_t = BYTE
uint16_t = WORD

char* createMSG(BYTE i,WORD port) 
{
    char *buff; 
    BYTE id, tmp;
    buff = (char*) calloc(1,6);
    tmp = 0;
    id = 2;
    memcpy(buff, &id, sizeof(BYTE));
    memcpy(buff+1, &i, sizeof(BYTE));
    memcpy(buff+2, &port, sizeof(WORD));
    memcpy(buff+2+2, &tmp, sizeof(WORD));
    memcpy(buff+2+2+1, &tmp, sizeof(WORD));
    printf("created SV_CON_REP: id: %d accept: %d port %d\n",*buff,*(buff+1),*        (buff+2));    return buff;
}

call:

createMSG(9,1234);  

printf result:

created SV_CON_REP: id: 2 accept: 9 port 210

(1234 = 0x04d2, where 0xd2=210)

you still not copy call & printf result, but your own comment

OTHER TIPS

How about *((uint_16*)(buff+2)) in printf?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top