Home > Back-end >  Do not use c memcpy the binary stream can be decoded?
Do not use c memcpy the binary stream can be decoded?

Time:10-24

Binary stream contain int type and char type, such as 0 and 1 is two char types, 2-5, 6-9 are two type int, 10-11 are two char types, in c + + and Qt, has not use memcpy function to decode method to them? So what to do?

CodePudding user response:

Byte to write their own operation cycle
Write a structure and structure assignment is used to implement
Are memcpy and no memcpy traversal
So the question to the
Why do you want to ask for it?

CodePudding user response:

QDataStream qt, but I guess it's internal memcpy is used to realize, but when users did not see the mem,

CodePudding user response:

//binary stream containing the int type and char type, such as 0 and 1 is two char types, 2-5, 6-9 are two type int, 10-11 are two char types, in c + + and Qt, do not use the memcpy function 
Char buf [12].
Char a, b;
Int c, d;
Char e, f;
A=buf [0].
B=buf [1].
C=(int) (
((unsigned int) buf [2]) & lt; <24 |
((unsigned int) buf [3]) & lt; <16 |
((unsigned int) buf [4]) & lt; <8 |
((unsigned int) buf [5])
);
D=(int) (
((unsigned int) buf [6]) & lt; <24 |
((unsigned int) buf [7]) & lt; <16 |
((unsigned int) buf [8]) & lt; <8 |
((unsigned int) buf [9])
);
E=buf [0].
F=buf [1].


C, d value if wrong, put the order of 2345 and 6789 (size)

CodePudding user response:

E=buf [10].
F=buf [11];

CodePudding user response:

This is called formatting,
Constructs a role is to give you a formatted,
Struct A
{
Char
Int
Int
Char
Char
}

A * pA=new A ();
PA=(A *) & amp; Buffer;

CodePudding user response:

reference 5 floor thousand dream life reply:
this call formatting,
Constructs a role is to give you a formatted,
Struct A
{
Char
Int
Int
Char
Char
}

A * pA=new A ();
PA=(A *) & amp; Buffer;

A * pA=new A ();
PA=(A *) & amp; Buffer;

Write wrong, memory leak
  • Related