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. Copy Byte information to structure

Copy Byte information to structure

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
5 Posts 5 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.
  • N Offline
    N Offline
    nitin3
    wrote on last edited by
    #1

    i have a structure and BYTE array typedef struct _my_struct { DWORD dwValue ; BYTE yValue1 ; BYTE yValue2 ; WORD wValue1 ; BYTE yValue3 ; WORD wValue2 ; BYTE yValue4 ; }mystruct ; BYTE Buf[10] = {"ab--c--d"} ; i need to copy a to yValue1, b to yValue2, -- to wValue1, c to yValue3, -- to wValue2, and d to yValue1. i tried the following code but its not working mystruct st ; memcpy( &st.yValue1 , Buf , 6 ) ; is any way to do that ?

    Thanks & Regards

    S C A C 4 Replies Last reply
    0
    • N nitin3

      i have a structure and BYTE array typedef struct _my_struct { DWORD dwValue ; BYTE yValue1 ; BYTE yValue2 ; WORD wValue1 ; BYTE yValue3 ; WORD wValue2 ; BYTE yValue4 ; }mystruct ; BYTE Buf[10] = {"ab--c--d"} ; i need to copy a to yValue1, b to yValue2, -- to wValue1, c to yValue3, -- to wValue2, and d to yValue1. i tried the following code but its not working mystruct st ; memcpy( &st.yValue1 , Buf , 6 ) ; is any way to do that ?

      Thanks & Regards

      S Offline
      S Offline
      SandipG
      wrote on last edited by
      #2

      nitin3 wrote:

      mystruct st ; memcpy( &st.yValue1 , Buf , 6 ) ; i

      do not use this unless you are sure about the alignment of your structure. If you are not aware of what is alignment read this Pragma packing[^]

      Regards, Sandip.

      1 Reply Last reply
      0
      • N nitin3

        i have a structure and BYTE array typedef struct _my_struct { DWORD dwValue ; BYTE yValue1 ; BYTE yValue2 ; WORD wValue1 ; BYTE yValue3 ; WORD wValue2 ; BYTE yValue4 ; }mystruct ; BYTE Buf[10] = {"ab--c--d"} ; i need to copy a to yValue1, b to yValue2, -- to wValue1, c to yValue3, -- to wValue2, and d to yValue1. i tried the following code but its not working mystruct st ; memcpy( &st.yValue1 , Buf , 6 ) ; is any way to do that ?

        Thanks & Regards

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        nitin3 wrote:

        is any way to do that ?

        Yes, using the recipe

        nitin3 wrote:

        copy a to yValue1, b to yValue2, -- to wValue1, c to yValue3, -- to wValue2, and d to yValue1.

        you proposed. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        1 Reply Last reply
        0
        • N nitin3

          i have a structure and BYTE array typedef struct _my_struct { DWORD dwValue ; BYTE yValue1 ; BYTE yValue2 ; WORD wValue1 ; BYTE yValue3 ; WORD wValue2 ; BYTE yValue4 ; }mystruct ; BYTE Buf[10] = {"ab--c--d"} ; i need to copy a to yValue1, b to yValue2, -- to wValue1, c to yValue3, -- to wValue2, and d to yValue1. i tried the following code but its not working mystruct st ; memcpy( &st.yValue1 , Buf , 6 ) ; is any way to do that ?

          Thanks & Regards

          A Offline
          A Offline
          Alan Balkany
          wrote on last edited by
          #4

          One problem is that you're copying 6 bytes, but the length of Buf is 8.

          1 Reply Last reply
          0
          • N nitin3

            i have a structure and BYTE array typedef struct _my_struct { DWORD dwValue ; BYTE yValue1 ; BYTE yValue2 ; WORD wValue1 ; BYTE yValue3 ; WORD wValue2 ; BYTE yValue4 ; }mystruct ; BYTE Buf[10] = {"ab--c--d"} ; i need to copy a to yValue1, b to yValue2, -- to wValue1, c to yValue3, -- to wValue2, and d to yValue1. i tried the following code but its not working mystruct st ; memcpy( &st.yValue1 , Buf , 6 ) ; is any way to do that ?

            Thanks & Regards

            C Offline
            C Offline
            cmk
            wrote on last edited by
            #5

            Aside from needing to pack the structure: #pragma pack(push, 1) typedef ...; #pragma pack(pop) and copying 8 bytes, not 6: memcpy(..., 8); you may also need to swap the WORD values after the copy depending on their represenstation in Buf and meaning in mystruct: memcpy(...); _swab(&Buf[2], &st.wValue1, 2); _swab(&Buf[5], &st.wValue2, 2);

            ...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack

            modified on Monday, September 22, 2008 6:37 PM

            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