C - Problems extracting data from buffer. Possibly endianess-related -
I'm having some difficulties in extracting data from buffers using memory.
First of all, I have variables in some buffer: variable char * msg to store in b> Then I try to remove that data (check before sending the UDP socket): As you can see, the values are not quite what I hope. Can anyone tell me what I am doing?
int l1_send (int device, const char * Buf, int length) {// variable to store the extracted data uint16_t id = 0; Uint16_t size = 0; Four messages [50]; Memcpy (& amp; id, & amp; buf, sizeof (id)); Memcpy (& amp; Size, & amp; buf + sizeof (ID), sizeof (size)); Int balance = ntohs (size) - (sizeof (id) + sizeof (size)); Printf ("l1_send (): \ n Finish:% d \ n", left); // -37041 // memcpy-ing correct (?) Offset mempi (& msg, and buff + size) size (size), 50); Msg [49] = '\ 0'; Printf ("id:% d \ n", ntohs (id)); // 8372 printf ("Size:% d \ n", ntohs (size)); // 37045 printf ("msg:% s \ n", msg); // $ _ Return 0; // for now}
Your indicator counter is incorrect. You can call & amp; Buf
, where you should just use buf
if it does not misunderstand, then I can not say anything:
#include & lt; Stdio.h & gt; Int main (int argc, char ** argv) {four fans [100]; Printf ("Buff:% P \ nBuff + 10:% P \ nand Buff + 10:% P \ n", Buff, Buff + 10, and Buff + 10); Return 0; }
Output (vary by platform, obviously)
Buff: 0xbf87a8bc Buff + 10: 0xbf87a8c6 And Buff + 10: 0xbf87aca4
The math type you've been growing with, which is the & amp; Buf is an indicator for the array of 100
; Not a simple char
s for char
address, therefore, & amp; Buff + 10
(in my sample) says, "Give me 10th array of 100 characters from now." Writing later is starting undetermined behavior as a result.
Your friend is here, BTW It will catch a heartbeat.
Update
May when I am here, even then fill in the whole word. It is also incorrect in l1_send
:
memcpy (& id, & amp; buf, sizeof (id)); // This ------ ^
And in the other areas after that you are using it in that function. You are locating a parameter pointer, there is no value inside it, I believe that you need buf
.
Comments
Post a Comment