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. Problem with parsing a BYTE*

Problem with parsing a BYTE*

Scheduled Pinned Locked Moved C / C++ / MFC
jsonhelpquestion
4 Posts 2 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.
  • _ Offline
    _ Offline
    __Cerb
    wrote on last edited by
    #1

    Here's my code int ParsePacket(BYTE *aPacket,short int iStartPos); void main() { BYTE aPacket[] = {0x59,0x89,0x59,0x59,0x23,0x59,0x59,0x59,0x89,0x59,0x00,0x54,0x65}; int l = ParsePacket(aPacket,5); printf("%d\n",l system("pause"); } int ParsePacket(BYTE *aPacket,short int iStartPos) { bool bFound = false; int iPos = 0; for (int i = 0;i < (sizeof(aPacket) - 1); i++) { printf("Test: %x\n",aPacket[i]); } return iPos; } This piece of code should output twelve values ( sizeof(aPacket) = 13 ), but instead it only outputs the first three bytes of aPacket... why?

    C 1 Reply Last reply
    0
    • _ __Cerb

      Here's my code int ParsePacket(BYTE *aPacket,short int iStartPos); void main() { BYTE aPacket[] = {0x59,0x89,0x59,0x59,0x23,0x59,0x59,0x59,0x89,0x59,0x00,0x54,0x65}; int l = ParsePacket(aPacket,5); printf("%d\n",l system("pause"); } int ParsePacket(BYTE *aPacket,short int iStartPos) { bool bFound = false; int iPos = 0; for (int i = 0;i < (sizeof(aPacket) - 1); i++) { printf("Test: %x\n",aPacket[i]); } return iPos; } This piece of code should output twelve values ( sizeof(aPacket) = 13 ), but instead it only outputs the first three bytes of aPacket... why?

      C Offline
      C Offline
      Curi0us_George
      wrote on last edited by
      #2

      TO get a hint as to the problem, you should have added an printf for "sizeof(aPacket)". In the context of the ParsePacket function, sizeof(aPacket) is 4, because ParsePacket recieves aPacket as a BYTE *, and on the 32-bit x86 architecture, all pointers are 4 bytes. The sizeof operator has no way of determining the length of an array pointed to by aPacket. i.e.: //----- #include #include int ParsePacket(BYTE *aPacket,short int iStartPos); void main() { BYTE aPacket[] = {0x59,0x89,0x59,0x59,0x23,0x59,0x59,0x59,0x89,0x59,0x00,0x54,0x65}; printf("size (in main): %i\n",sizeof(aPacket)); int l = ParsePacket(aPacket,5); printf("%d\n",l); system("pause"); } int ParsePacket(BYTE * aPacket,short int iStartPos) { bool bFound = false; int iPos = 0; printf("size (in ParsePacket): %i\n",sizeof(aPacket)); for (int i = 0;i < (sizeof(aPacket) - 1); i++) { printf("Test: %x\n",aPacket[i]); } return iPos; } //----- outputs: ----- size (in main): 13 size (in ParsePacket): 4 Test: 59 Test: 89 Test: 59 0 Press any key to continue . . . ----- You need to pass the length of the array to the ParsePacket method. i.e.: //----- #include #include void ParsePacket(BYTE *aPacket,int aPacketLength); void main() { BYTE aPacket[] = {0x59,0x89,0x59,0x59,0x23,0x59,0x59,0x59,0x89,0x59,0x00,0x54,0x65}; ParsePacket(aPacket,sizeof(aPacket)); system("pause"); } void ParsePacket(BYTE * aPacket,int aPacketLength) { for (int i = 0; i < aPacketLength; i++) { printf("Test: %x\n",aPacket[i]); } } //----- outputs: ----- Test: 59 Test: 89 Test: 59 Test: 59 Test: 23 Test: 59 Test: 59 Test: 59 Test: 89 Test: 59 Test: 0 Test: 54 Test: 65 Press any key to continue . . . -----

      _ 1 Reply Last reply
      0
      • C Curi0us_George

        TO get a hint as to the problem, you should have added an printf for "sizeof(aPacket)". In the context of the ParsePacket function, sizeof(aPacket) is 4, because ParsePacket recieves aPacket as a BYTE *, and on the 32-bit x86 architecture, all pointers are 4 bytes. The sizeof operator has no way of determining the length of an array pointed to by aPacket. i.e.: //----- #include #include int ParsePacket(BYTE *aPacket,short int iStartPos); void main() { BYTE aPacket[] = {0x59,0x89,0x59,0x59,0x23,0x59,0x59,0x59,0x89,0x59,0x00,0x54,0x65}; printf("size (in main): %i\n",sizeof(aPacket)); int l = ParsePacket(aPacket,5); printf("%d\n",l); system("pause"); } int ParsePacket(BYTE * aPacket,short int iStartPos) { bool bFound = false; int iPos = 0; printf("size (in ParsePacket): %i\n",sizeof(aPacket)); for (int i = 0;i < (sizeof(aPacket) - 1); i++) { printf("Test: %x\n",aPacket[i]); } return iPos; } //----- outputs: ----- size (in main): 13 size (in ParsePacket): 4 Test: 59 Test: 89 Test: 59 0 Press any key to continue . . . ----- You need to pass the length of the array to the ParsePacket method. i.e.: //----- #include #include void ParsePacket(BYTE *aPacket,int aPacketLength); void main() { BYTE aPacket[] = {0x59,0x89,0x59,0x59,0x23,0x59,0x59,0x59,0x89,0x59,0x00,0x54,0x65}; ParsePacket(aPacket,sizeof(aPacket)); system("pause"); } void ParsePacket(BYTE * aPacket,int aPacketLength) { for (int i = 0; i < aPacketLength; i++) { printf("Test: %x\n",aPacket[i]); } } //----- outputs: ----- Test: 59 Test: 89 Test: 59 Test: 59 Test: 23 Test: 59 Test: 59 Test: 59 Test: 89 Test: 59 Test: 0 Test: 54 Test: 65 Press any key to continue . . . -----

        _ Offline
        _ Offline
        __Cerb
        wrote on last edited by
        #3

        I didn't know that Thanks a lot :-)

        C 1 Reply Last reply
        0
        • _ __Cerb

          I didn't know that Thanks a lot :-)

          C Offline
          C Offline
          Curi0us_George
          wrote on last edited by
          #4

          You're very welcome. Glad I could help. :)

          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