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 160881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:06:49+00:00 2026-05-11T11:06:49+00:00

I have a function that generates a CRC check byte based on the content

  • 0

I have a function that generates a CRC check byte based on the content of any packet.The problem is in translating the function from C++ to C#

C++ code:

unsigned char GenerateCheckByte( char* packet, int length, unsigned long seed ) { if( !packet ) return 0; unsigned long checksum = 0xFFFFFFFF; length &= 0x7FFF; char* ptr = packet; unsigned long moddedseed = seed << 8; for( int i = 0; i < length; i++ )     checksum = ( checksum >> 8 ) ^ table[moddedseed + ( ( *(ptr++) ^ checksum ) & 0xFF )]; unsigned char result = ( (checksum>>24)&0xFF ) + ( (checksum>>8)&0xFF ) + ( (checksum>>16)&0xFF ) + ( checksum&0xFF ); return result; } 

the char*(packet) can also be defined as LPBYTE,the idea is that the value assigned to *packet is assigned to *ptr and as you see *ptr increases.Meaning a byte array is passed in and by increasing the value of the pointer it goes to the next byte.

I tried to do it in C# and failed many times.After some hard work I figured out some code,but i can’t execute it 😕

C# code

    public static unsafe byte GenerateCheckByte(byte *packet, int length, UInt32 seed )     {         if (*packet == 0)         return 0;         UInt32 checksum = 0xFFFFFFFF;         length &= 0x7FFF;         byte *ptr = packet;         UInt32 moddedseed = seed << 8;         for (int i = 0; i < length; i++)             checksum = ( checksum >> 8 ) ^ Table.table[moddedseed + ( ( *(ptr++) ^ checksum ) & 0xFF )];         byte result = (byte)(( (checksum>>24)&0xFF ) + ( (checksum>>8)&0xFF ) + ( (checksum>>16)&0xFF ) + ( checksum&0xFF ));         return result;     } 

It doesn’t look that bad,but I can’t call it

  unsafe   {       packetBuffer[5] = Functions.GenerateCheckByte(&packetBuffer[0], 18, packet.seedCRC);   } 

error: ‘You can only take the address of an unfixed expression inside of a fixed statement initializer’

Please note

packetbuffer in both C++ and C# application is byte[] packetBuffer = new byte[18];

  • 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. 2026-05-11T11:06:50+00:00Added an answer on May 11, 2026 at 11:06 am

    You could make the method accept a byte array:

    public static unsafe byte GenerateCheckByte(byte[] packetArray, int length, UInt32 seed) {     fixed(byte *packet = packetArray)     {         ... etc     } } 

    It’s better to keep the unsafe stuff hidden away as much as possible behind managed interfaces.

    Then calling it would be easy:

    packetBuffer[5] = Functions.GenerateCheckByte(packetBuffer, 18, ... 

    In fact, it would be better to write GenerateCheckByte to operate on an array anyway, instead of delving into unsafe techniques:

    public static unsafe byte GenerateCheckByte(byte[] packet, int length, UInt32 seed ) {     if (packet == null)         throw new ArgumentNullException('packet'); // the right way in C#      UInt32 checksum = 0xFFFFFFFF;     length &= 0x7FFF;      UInt32 moddedseed = seed << 8;     for (int i = 0; i < length; i++)         checksum = ( checksum >> 8 ) ^ Table.table[moddedseed + ( ( packet[i] ^ checksum ) & 0xFF )];     byte result = (byte)(( (checksum>>24)&0xFF ) + ( (checksum>>8)&0xFF ) + ( (checksum>>16)&0xFF ) + ( checksum&0xFF ));     return result; } 

    Write the simplest, safest implementation you can, and only mess with pointers if you find a bottleneck in profiling.

    Are you just translating a lot of existing C/C++ into C#? There’s little point doing that unless you get some new safety/maintainability from it. 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Prelude: It pays to understand the difference between precompiler directives… May 11, 2026 at 11:51 pm
  • Editorial Team
    Editorial Team added an answer I think a fair argument is also that not leaving… May 11, 2026 at 11:51 pm
  • Editorial Team
    Editorial Team added an answer An introductory article from CP A JSON parser implemented using… May 11, 2026 at 11:51 pm

Related Questions

I have a function that receives three different people objects and generates a new
I have a PHP application and a need to generate a PDF with the
I have a function which gets a key from the user and generates a
I've heard from a variety of places that global variables are inherently nasty and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.