what I'm trying is to convert C++ piece of code
while (n--) if (c & 0x80000000) c = (c << 1) ^ p2; else c <<= 1;
into c#
the whole code in c++ is:
#include "stdafx.h"
int main()
{
unsigned long c, c2, p2, pol = 0xEDB88320;
long n, k;
{
printf("CRC32 Adjuster (c) 2001 by RElf @ HHT/2\n");
printf("Length of data: "); scanf_s("%ld", &n);
printf("Offset to patch: "); scanf_s("%ld", &k);
n = (n - k) << 3;
printf("Current CRC32: 0x"); scanf_s("%x", &c);
printf("Desired CRC32: 0x"); scanf_s("%x", &c2);
c ^= c2;
p2 = (pol << 1) | 1;
while (n--) if (c & 0x80000000) c = (c << 1) ^ p2; else c <<= 1;
printf("XOR masks:%02X%02X%02X%02X\n", c & 0xff, (c >> 8) & 0xff, (c >> 16) & 0xff, c >> 24);
}
return 0;
}
what I have tried while translating into c#:
using System;
namespace crc323_fake
{
class Program
{
static void Main(string[] args)
{
long c, c2, p2, pol = 0xEDB88320;
long n, k;
{
n = 3440;
k = 3436;
n = (n - k) << 3;
c = 0x73CBFFB5;
c2 = 0x7D096252;
c ^= c2;
p2 = (pol << 1) | 1;
while (n != 0)
{
n = n - 1;
if( c & 0x80000000 ) // getting error here can't implicitly convert long to bool
{
}
}
Console.WriteLine("XOR masks:%02X%02X%02X%02X\n", c & 0xff, (c >> 8) & 0xff, (c >> 16) & 0xff, c >> 24);
while (1) ;
}
}
}
}
I got rid of print statements and gave the straight value to the variables, and I'm stuck with
while (n != 0)
{
n = n - 1;
if( c & 0x80000000 )
{
}
}
if( c & 0x80000000 ) gives me the error "can't implicitly convert long to bool" sorry if the question seems newbie, I'm really new at c#
In C++, non-nul integers convert to true.
So you have to be explicit here:
if (c & 0x80000000)
becomes
if ((c & 0x80000000) != 0)
User contributions licensed under CC BY-SA 3.0