Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have two objects, Header, and DF. lets say

header = CCCCCC7E

and

DF = 01020304,

shouldnt the value of the buffer be CCCCCC7E01020304?

for some reason when i printed it i got:

7EFFFFFFCCFFFFFFCCFFFFFFCCFFFFFFCCFFFFFFCCFFFFFFCCFFFFFFCCFFFFFFCCFFFFFFCC00FFFF FFCC00000000000004030201FFFFFF8967341200000000

this is how i printed it:

for (int i = 0; i < sizeof(buffer); i++)
{ printf("%02X", buffer[i]); }

this is the code:

    struct Header header;
    struct Data_Format DF;
    unsigned char buffer[TOTAL_SIZE];

    header.Start = 0x7E;
    header.Options = 0x00;
    header.PacketLength = 0x00;
    header.VCP = 0x00;
    header.Reserved = 0x00;
    header.Return = 0x00;

    DF.Address = 0x01020304; //real value: NULL
    DF.Result = 0x1234; //real value: NULL
    DF.Size = 0x6789; //real value: NULL

    memcpy(buffer,&header, sizeof(Header));
    memcpy(buffer+sizeof(Header), &DF, sizeof(Data_Format));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
286 views
Welcome To Ask or Share your Answers For Others

1 Answer

From the partial code given it doesn't give much idea but Some point need to do.

Do a memset always before coping to the buffer.

memset(buffer, 0, sizeof(buffer)) 

This will prevent you to get the junk.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...