Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8110165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:39:18+00:00 2026-06-06T01:39:18+00:00

How to correctly convert the following C++ into C#? //——————————————————————– // Calculate a 16-bit

  • 0

How to correctly convert the following C++ into C#?

//--------------------------------------------------------------------
// Calculate a 16-bit Cycle Redundency Check (CRC) value on a block of //data.
//
// Params:
// pData : Pointer to data to calculate CRC.
// dwSize : Size of data in bytes.
//
// Return:
// 16-bit CRC value.
//
// Notes:
//--------------------------------------------------------------------
private WORD i_pwCalculateCRC16Continuation(PBYTE pData, WORD dwSize, WORD wCRC)
{
    // high byte of CRC initialized
    BYTE cCRCHi = (BYTE) ((wCRC >> 8) & 0xFF);
    // low byte of CRC initialized
    BYTE cCRCLo = (BYTE) (wCRC & 0xFF);
    // will index into CRC lookup table
    BYTE cIndex;    
    while (dwSize--) // step through each byte of data
    {
        cIndex = cCRCHi ^ *pData++; // calculate the CRC
        cCRCHi = cCRCLo ^ m_cCRCHiArray[cIndex];
        cCRCLo = m_cCRCLoArray[cIndex];
    }
    return (cCRCHi << 8) + cCRCLo;
}

I found a tool that converted it but it were not perfect and it does not understand the context.

private ushort i_pwCalculateCRC16Continuation(ref byte pData, ushort dwSize, ushort wCRC)
{
    byte cCRCHi = (byte) ((wCRC >> 8) & 0xFF);
    byte cCRCLo = (byte) (wCRC & 0xFF);
    byte cIndex;

    while (dwSize-- > 0)
    {
        // Cannot convert source type 'int' to target type 'byte'
        cIndex = cCRCHi ^ pData++;
        // Cannot apply indexing to an expression of type 'byte'
        cCRCHi = cCRCLo ^ m_cCRCHiArray[cIndex];
        // Cannot apply indexing to an expression of type 'byte'
        cCRCLo = m_cCRCLoArray[cIndex];
    }
    // Cannot convert expression type 'int' to return type 'ushort'
    return (cCRCHi << 8) + cCRCLo;
}

The CRC concept and bit wise operations are little bit unfamiliar for me and I do not understand the above code well. Hence I do not know how to “fix” it.

Thanks

EDIT:
Missed the following variables.

private static byte[] m_cCRCHiArray = {
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,  0x81, 0x40
};

private static byte[] m_cCRCLoArray = {
    0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0x80, 0x40
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:39:19+00:00Added an answer on June 6, 2026 at 1:39 am

    Try this:

    private static byte[] m_cCRCHiArray = { 
        0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,  0x81, 0x40 
    };
    
    private static byte[] m_cCRCLoArray = { 
        0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0x80, 0x40 
    };
    
    private ushort i_pwCalculateCRC16Continuation(byte[] data, ushort wCRC)
    {
        byte cCRCHi = (byte)((wCRC >> 8) & 0xFF);
        byte cCRCLo = (byte)(wCRC & 0xFF);
    
        byte cIndex;
    
        for (int i=0; i < data.Length; i++)
        {
            cIndex = (byte)(cCRCHi ^ data[i]);
            cCRCHi = (byte)(cCRCLo ^ m_cCRCHiArray[cIndex]);
            cCRCLo = m_cCRCLoArray[cIndex];
        }
    
        return (byte)((cCRCHi << 8) + cCRCLo);
    }
    

    It isn’t obviously wrong at first glance 😉

    I’ve not altered any of the hungarian notation, except for changing pData to data

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can we convert the following 64 bit binary into the long equivalent; 01111101
Hey everyone, I need to convert the following format into a new format using
Is is possible to convert following into a simpler more readable linq or lambda
How do I convert the following for -loop containing an if / else into
how do I convert a reference to a pointer correctly? The code below gives
I'm trying to convert the following NSString api call to a NSURL object: http://beta.com/api/token=
I need to read a bi-dimensional array and convert the values into an HTML
I have the following option text that I want to put into a variable
How can I pass an array of filenames into the following method: public static
I have the following code that correctly generates the CKEditor: <script> $(function(){ $(#newWrite_body).ckeditor({ extraPlugins

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.