A beginer's qustion
-
Hi everybody. I'm developing an IC card handset device application with standard C language.I'm a new comer to C language.I have a question Look at the funtion definition :
unsigned short dt_recv(unsigned char *data, int *datalen) Input parameters: Data : to receive data buffer Output parameters: Datalen:receive data buffer size Return values: none. Communication Protocol : data[0]=command head; ... data[3]=high of length; data[4]=low of length; ...
Data[3] and Data[4] store a Int16 value.I have problem when I try to get the value.Here is my code:short i; memcpy(&Data[3],&i,2);
But it seems that I did not get the right value.I guess I'm in the wrong way.I'm not familiar with memory operation. Can any one give me the right way?Any idea will be appropriate. -
Hi everybody. I'm developing an IC card handset device application with standard C language.I'm a new comer to C language.I have a question Look at the funtion definition :
unsigned short dt_recv(unsigned char *data, int *datalen) Input parameters: Data : to receive data buffer Output parameters: Datalen:receive data buffer size Return values: none. Communication Protocol : data[0]=command head; ... data[3]=high of length; data[4]=low of length; ...
Data[3] and Data[4] store a Int16 value.I have problem when I try to get the value.Here is my code:short i; memcpy(&Data[3],&i,2);
But it seems that I did not get the right value.I guess I'm in the wrong way.I'm not familiar with memory operation. Can any one give me the right way?Any idea will be appropriate. -
Hi everybody. I'm developing an IC card handset device application with standard C language.I'm a new comer to C language.I have a question Look at the funtion definition :
unsigned short dt_recv(unsigned char *data, int *datalen) Input parameters: Data : to receive data buffer Output parameters: Datalen:receive data buffer size Return values: none. Communication Protocol : data[0]=command head; ... data[3]=high of length; data[4]=low of length; ...
Data[3] and Data[4] store a Int16 value.I have problem when I try to get the value.Here is my code:short i; memcpy(&Data[3],&i,2);
But it seems that I did not get the right value.I guess I'm in the wrong way.I'm not familiar with memory operation. Can any one give me the right way?Any idea will be appropriate. -
Hi everybody. I'm developing an IC card handset device application with standard C language.I'm a new comer to C language.I have a question Look at the funtion definition :
unsigned short dt_recv(unsigned char *data, int *datalen) Input parameters: Data : to receive data buffer Output parameters: Datalen:receive data buffer size Return values: none. Communication Protocol : data[0]=command head; ... data[3]=high of length; data[4]=low of length; ...
Data[3] and Data[4] store a Int16 value.I have problem when I try to get the value.Here is my code:short i; memcpy(&Data[3],&i,2);
But it seems that I did not get the right value.I guess I'm in the wrong way.I'm not familiar with memory operation. Can any one give me the right way?Any idea will be appropriate.Xiaoming Qian wrote:
short i; memcpy(&Data[3],&i,2); But it seems that I did not get the right value.I guess I'm in the wrong way.I'm
Are you wanting the value of
Data[3]
ori
? The signature formemcpy()
is:void *memcpy( void *dest, const void *src, size_t count );
So what you have is writing from
i
toData[3]
.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Hi everybody. I'm developing an IC card handset device application with standard C language.I'm a new comer to C language.I have a question Look at the funtion definition :
unsigned short dt_recv(unsigned char *data, int *datalen) Input parameters: Data : to receive data buffer Output parameters: Datalen:receive data buffer size Return values: none. Communication Protocol : data[0]=command head; ... data[3]=high of length; data[4]=low of length; ...
Data[3] and Data[4] store a Int16 value.I have problem when I try to get the value.Here is my code:short i; memcpy(&Data[3],&i,2);
But it seems that I did not get the right value.I guess I'm in the wrong way.I'm not familiar with memory operation. Can any one give me the right way?Any idea will be appropriate.Xiaoming Qian wrote:
short i; memcpy(&Data[3],&i,2);
On Intel machines, i is stored with the least-significant byte first. If you use memcpy like this then Data[3] == least-significant byte of i Data[4] == most-significant byte of i To get the value back. something like: short i = (short)((unsigned short)Data[4] << 8) | (unsigned short)Data[3]; or short i; memcpy(&i,&Data[3],2);
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
Hi everybody. I'm developing an IC card handset device application with standard C language.I'm a new comer to C language.I have a question Look at the funtion definition :
unsigned short dt_recv(unsigned char *data, int *datalen) Input parameters: Data : to receive data buffer Output parameters: Datalen:receive data buffer size Return values: none. Communication Protocol : data[0]=command head; ... data[3]=high of length; data[4]=low of length; ...
Data[3] and Data[4] store a Int16 value.I have problem when I try to get the value.Here is my code:short i; memcpy(&Data[3],&i,2);
But it seems that I did not get the right value.I guess I'm in the wrong way.I'm not familiar with memory operation. Can any one give me the right way?Any idea will be appropriate.Here "data[]" is an array. so "data" itself an a address. And instead of using "&Data[3]" use "Data[3]" in "memcpy" function That is: short i; memcpy(Data[3],&i,2);
-
Hi everybody. I'm developing an IC card handset device application with standard C language.I'm a new comer to C language.I have a question Look at the funtion definition :
unsigned short dt_recv(unsigned char *data, int *datalen) Input parameters: Data : to receive data buffer Output parameters: Datalen:receive data buffer size Return values: none. Communication Protocol : data[0]=command head; ... data[3]=high of length; data[4]=low of length; ...
Data[3] and Data[4] store a Int16 value.I have problem when I try to get the value.Here is my code:short i; memcpy(&Data[3],&i,2);
But it seems that I did not get the right value.I guess I'm in the wrong way.I'm not familiar with memory operation. Can any one give me the right way?Any idea will be appropriate.I believe you should use a lower-case "D" in "Data". C is a case-sensitive language. For example, use
memcpy(&i,&data[3],2);
instead ofmemcpy(&i,&Data[3],2);
As others have noted, there could be other problems because you're reading from an array of unsigned chars into a short. -- modified at 14:06 Wednesday 20th June, 2007 -
Here "data[]" is an array. so "data" itself an a address. And instead of using "&Data[3]" use "Data[3]" in "memcpy" function That is: short i; memcpy(Data[3],&i,2);
Wrong. The first argument to
memcpy()
needs to be an address, of whichData[3]
is not.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
I believe you should use a lower-case "D" in "Data". C is a case-sensitive language. For example, use
memcpy(&i,&data[3],2);
instead ofmemcpy(&i,&Data[3],2);
As others have noted, there could be other problems because you're reading from an array of unsigned chars into a short. -- modified at 14:06 Wednesday 20th June, 2007Erik Midtskogen wrote:
I believe you should use a lower-case "D" in "Data".
Umm, the compiler would have told him this (i.e., syntax error). :rolleyes:
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Erik Midtskogen wrote:
I believe you should use a lower-case "D" in "Data".
Umm, the compiler would have told him this (i.e., syntax error). :rolleyes:
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
I was assuming that he may have been getting a compiler error and not known why. Probably we should ask him what sort compiler message or other symptom of a problem he is seeing.