我正在尝试转换c++代码
while (n--) if (c & 0x80000000) c = (c << 1) ^ p2; else c <<= 1;
c#到
c++中的全部代码是:
#include "stdafx.h"
int main()
{
unsigned long c, c2, p2, pol = 0xEDB88320;
long n, k;
{
printf("CRC32 Adjuster (c) 2001 by RElf @ HHT/2n");
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%02Xn", c & 0xff, (c >> 8) & 0xff, (c >> 16) & 0xff, c >> 24);
}
return 0;
}
我在翻译成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%02Xn", c & 0xff, (c >> 8) & 0xff, (c >> 16) & 0xff, c >> 24);
while (1) ;
}
}
}
}
我去掉了print语句,直接给变量赋值,所以我只能用
while (n != 0)
{
n = n - 1;
if( c & 0x80000000 )
{
}
}
if(c &0 x80000000)给我一个错误"不能隐式地将long转换为bool"。对不起,如果这个问题看起来很新手,我真的是c#新手
在c++中,非空整数转换为true。
这里必须明确:
if (c & 0x80000000)
是
if ((c & 0x80000000) != 0)
演示