CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2003
    Posts
    235

    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

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    It's pity you chose to use CByteArray. Use std::vector and the solution's simple:
    Code:
    std::vector<BYTE> indmessage;
    indmessage.push_back(0xf2);
    indmessage.push_back(i_enter_station);
    indmessage.push_back(0x00);
    indmessage.push_back(0x00);
    indmessage.push_back(0x01);
    indmessage.push_back(0x00);
    indmessage.push_back(0x02);
    indmessage.push_back(0x00);
    indmessage.push_back(0x03);
    indmessage.push_back(0x00);
    int nnumberofbytes=indmessage.GetSize();
    test_crc = calc_crc16(&indmessage[0], 5);
    I can't answer for CByteArray, though - I try to steer clear of inferior classes.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Pass CbyteArray to a function?

    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

  4. #4
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    test_crc = calc_crc16(indmessage, 5);

    WORD calc_crc16(const BYTE* pBuffer, const int nLength);

    looks like you have a bug there, since there are more than 5 elements in you indmessage array.

  5. #5
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236

    Re: Re: Pass CbyteArray to a function?

    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> ?

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Re: Re: Pass CbyteArray to a function?

    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

  7. #7
    Join Date
    Sep 2002
    Posts
    1,747
    If you look in afxcoll.h, you will notice that CByteArray is a fully defined class, not a typedef, with no generic components. The only advantage it might have over std::vector is serialization support, but I personally tend to avoid MFC serialization techniques (again very old; predate modern c++ RTTI -- which is still rather lacking -- and other advances which place limits on its use) over the more robust methods with better type safety and support for generic parameterization.
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  8. #8
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236

    Re: Re: Re: Re: Pass CbyteArray to a function?

    Originally posted by Paul McKenzie
    Also, the CArray<> template takes two arguments.
    From AfxTmpl.h
    template<class TYPE, class ARG_TYPE = const TYPE&>
    class CArray : public CObject
    The second is optional.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured