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