Copy Byte information to structure
-
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
-
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
-
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
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] -
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
One problem is that you're copying 6 bytes, but the length of Buf is 8.
-
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
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