Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. A beginer's qustion

A beginer's qustion

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformancehelp
10 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    Xiaoming Qian
    wrote on last edited by
    #1

    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.

    X K D M S 6 Replies Last reply
    0
    • X Xiaoming Qian

      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.

      X Offline
      X Offline
      xyecloudy
      wrote on last edited by
      #2

      are you a chinese boy?:laugh:

      1 Reply Last reply
      0
      • X Xiaoming Qian

        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.

        K Offline
        K Offline
        kakan
        wrote on last edited by
        #3

        My suggestion (not tested): short i; i = (short) ((data[3] << 8) | data[4]);

        Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

        1 Reply Last reply
        0
        • X Xiaoming Qian

          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.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          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] or i? The signature for memcpy() is:

          void *memcpy( void *dest, const void *src, size_t count );

          So what you have is writing from i to Data[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

          1 Reply Last reply
          0
          • X Xiaoming Qian

            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.

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • X Xiaoming Qian

              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.

              S Offline
              S Offline
              Shatak Gurukar
              wrote on last edited by
              #6

              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);

              D 1 Reply Last reply
              0
              • X Xiaoming Qian

                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.

                E Offline
                E Offline
                Erik Midtskogen
                wrote on last edited by
                #7

                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 of memcpy(&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

                D 1 Reply Last reply
                0
                • S Shatak Gurukar

                  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);

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  Wrong. The first argument to memcpy() needs to be an address, of which Data[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

                  1 Reply Last reply
                  0
                  • E Erik Midtskogen

                    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 of memcpy(&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

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    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

                    E 1 Reply Last reply
                    0
                    • D David Crow

                      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

                      E Offline
                      E Offline
                      Erik Midtskogen
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups