Pass CbyteArray to a function?
Hello,
How do I pass the contents of this CbyteArray to this function? I have declared a CbyteArray in Visual C++ the following manner:
CByteArray indmessage;
indmessage.Add(0xf2);
indmessage.Add(i_enter_station);
indmessage.Add(0x00);
indmessage.Add(0x00);
indmessage.Add(0x01);
indmessage.Add(0x00);
indmessage.Add(0x02);
indmessage.Add(0x00);
indmessage.Add(0x03);
indmessage.Add(0x00);
int nnumberofbytes=indmessage.GetSize();
test_crc = calc_crc16(indmessage, 5);
I have a function (referred to in a header file) that consists of the following to compute a CRC-16:
#include "stdafx.h"
WORD calc_crc16(const BYTE* pBuffer, const int nLength)
{
static const WORD CRC16_TABLE[16] =
{
0x0000, 0xCC01, 0xD801, 0x1400,
0xF001, 0x3C00, 0x2800, 0xE401,
0xA001, 0x6C00, 0x7800, 0xB401,
0x5000, 0x9C01, 0x8801, 0x4400
};
register WORD wCrc = 0;
register WORD wTemp;
register int nIndex;
for(nIndex = 0; nIndex < nLength; nIndex++, pBuffer++)
{
/* lower 4 bits */
wTemp = CRC16_TABLE[wCrc & 0x000F];
wCrc = (wCrc >> 4) & 0x0FFF;
wCrc = wCrc ^ wTemp ^ CRC16_TABLE[*pBuffer & 0x000F];
/* upper 4 bits */
wTemp = CRC16_TABLE[wCrc & 0x000F];
wCrc = (wCrc >> 4) & 0x0FFF;
wCrc = wCrc ^ wTemp ^ CRC16_TABLE[(*pBuffer >> 4) & 0x000F];
}
return wCrc;
}
My question is: how do I pass the 10 bytes I added to my CbyteArray to the CRC-16 function so that the function can compute the CRC-16 value of all 10 bytes?
I attempted this:
WORD test_crc = calc_crc16(indmessage, nnumberofbytes);
But obviously, it didn’t work.
Thank you!
Electroskill
Re: Pass CbyteArray to a function?
Quote:
Originally posted by Electroskill
My question is: how do I pass the 10 bytes I added to my CbyteArray to the CRC-16 function so that the function can compute the CRC-16 value of all 10 bytes?
Use the GetData() member function. It returns a pointer to the data. It is equivalent to Graham's answer of passing the address of the first element in a vector.
Also, CByteArray goes way back and should not be used in a C++ program in this day and age, not even in an MFC-based program. It has been superseded by the C++ standard classes (std::vector), and is even superseded by the MFC CArray<> template class.
Regards,
Paul McKenzie
Re: Re: Pass CbyteArray to a function?
Quote:
Originally posted by Paul McKenzie
It has been superseded by the C++ standard classes (std::vector), and is even superseded by the MFC CArray<> template class.
I thought CByteArray was just a typedef of CArray<BYTE> ?
Re: Re: Re: Pass CbyteArray to a function?
Quote:
Originally posted by mwilliamson
I thought CByteArray was just a typedef of CArray<BYTE> ?
CByteArray goes back to the days of Visual C++ 1.0 when there were no templates, therefore it couldn't be a typedef for CArray<BYTE>.
Also, the CArray<> template takes two arguments.
Regards,
Paul McKenzie
Re: Re: Re: Re: Pass CbyteArray to a function?
Quote:
Originally posted by Paul McKenzie
Also, the CArray<> template takes two arguments.
Quote:
From AfxTmpl.h
template<class TYPE, class ARG_TYPE = const TYPE&>
class CArray : public CObject
The second is optional.