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. BYTE array and lists

BYTE array and lists

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structuresquestion
6 Posts 3 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.
  • M Offline
    M Offline
    mcsherry
    wrote on last edited by
    #1

    Hi, My program needs to take a pointer to BYTE array (unsigned char*) and convert it into a STL list so that each BYTE in the array has its own element in the list, i.e. if the array has hundred bytes then the list needs to have a hundred entries, at present when I try to do this each element in my list points to the entire BYTE array, when what I really need is copies of each single BYTE in its own part of the list. Does anyone know how I can do this? cheers, Andy

    A 1 Reply Last reply
    0
    • M mcsherry

      Hi, My program needs to take a pointer to BYTE array (unsigned char*) and convert it into a STL list so that each BYTE in the array has its own element in the list, i.e. if the array has hundred bytes then the list needs to have a hundred entries, at present when I try to do this each element in my list points to the entire BYTE array, when what I really need is copies of each single BYTE in its own part of the list. Does anyone know how I can do this? cheers, Andy

      A Offline
      A Offline
      Antony M Kancidrowski
      wrote on last edited by
      #2

      You could have something like

      std::list<char> m_STLList;
      for (int nByteCount = 0; nByteCount < entries_in_array; nByteCount++)
      {
        char* pByte = (pByteArray + nByteCount);
        m_STLList.push_back(*pByte);
      }

      Hope this helps Ant. I'm hard, yet soft.
      I'm coloured, yet clear.
      I'm fruity and sweet.
      I'm jelly, what am I? Muse on it further, I shall return!
      - David Williams (Little Britain)

      M 1 Reply Last reply
      0
      • A Antony M Kancidrowski

        You could have something like

        std::list<char> m_STLList;
        for (int nByteCount = 0; nByteCount < entries_in_array; nByteCount++)
        {
          char* pByte = (pByteArray + nByteCount);
          m_STLList.push_back(*pByte);
        }

        Hope this helps Ant. I'm hard, yet soft.
        I'm coloured, yet clear.
        I'm fruity and sweet.
        I'm jelly, what am I? Muse on it further, I shall return!
        - David Williams (Little Britain)

        M Offline
        M Offline
        mcsherry
        wrote on last edited by
        #3

        cheers Ant for your reply. Unfortunatly when I use: char* pByte = (pByteArray + nByteCount); all I get is pByte pointing to the actual data in memory instead of making a copy of that particular byte. Andy,

        A 1 Reply Last reply
        0
        • M mcsherry

          cheers Ant for your reply. Unfortunatly when I use: char* pByte = (pByteArray + nByteCount); all I get is pByte pointing to the actual data in memory instead of making a copy of that particular byte. Andy,

          A Offline
          A Offline
          Antony M Kancidrowski
          wrote on last edited by
          #4

          It is the line that adds it to the list that makes the copy (notice the *pByte)

          m_STLList.push_back(*pByte);

          if you like it can be changed to

          char Byte = *(pByteArray + nByteCount);
          m_STLList.push_back(Byte);

          or

          char Byte = pByteArray[nByteCount];
          m_STLList.push_back(Byte);

          Ant. I'm hard, yet soft.
          I'm coloured, yet clear.
          I'm fruity and sweet.
          I'm jelly, what am I? Muse on it further, I shall return!
          - David Williams (Little Britain)

          M 1 Reply Last reply
          0
          • A Antony M Kancidrowski

            It is the line that adds it to the list that makes the copy (notice the *pByte)

            m_STLList.push_back(*pByte);

            if you like it can be changed to

            char Byte = *(pByteArray + nByteCount);
            m_STLList.push_back(Byte);

            or

            char Byte = pByteArray[nByteCount];
            m_STLList.push_back(Byte);

            Ant. I'm hard, yet soft.
            I'm coloured, yet clear.
            I'm fruity and sweet.
            I'm jelly, what am I? Muse on it further, I shall return!
            - David Williams (Little Britain)

            M Offline
            M Offline
            mcsherry
            wrote on last edited by
            #5

            Hi Ant, I see what you mean now (I'm being a bit dense today! X| )

            R 1 Reply Last reply
            0
            • M mcsherry

              Hi Ant, I see what you mean now (I'm being a bit dense today! X| )

              R Offline
              R Offline
              Robert A T Kaldy
              wrote on last edited by
              #6

              Code short and fast: BYTE* byte_array; std::list<char> char_list; char_list.assign(byte_array, byte_array + number_of_entries_in_byte_array); :-D Robert-Antonio "A flower walked around a meadow. She saw a beautiful human and plucked off his head."

              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