奇怪的是我如何在C++中完成它,而不是在C#中.
为了清楚起见,我将两个函数粘贴在C++中,然后粘贴到C#中,并使用注释"// error"标记C#代码中有问题的行.这两个函数的作用是对参数进行编码,然后将其添加到名为byte1seeds的全局变量中.
这些是C++中的函数
//Global var: unsigned char byte1seeds[3]; unsigned long GenerateValue( unsigned long * Ptr ) { unsigned long val = *Ptr; for( int i = 0; i < 32; i++ ) val = (((((((((((val >> 2)^val) >> 2)^val) >> 1)^val) >> 1)^val) >> 1)^val)&1)|((((val&1) << 31)|(val >> 1))&0xFFFFFFFE); return ( *Ptr = val ); } void SetupCountByte( unsigned long seed ) { if( seed == 0 ) seed = 0x9ABFB3B6; unsigned long mut = seed; unsigned long mut1 = GenerateValue( &mut ); unsigned long mut2 = GenerateValue( &mut ); unsigned long mut3 = GenerateValue( &mut ); GenerateValue( &mut ); unsigned char byte1 = (mut&0xFF)^(mut3&0xFF); unsigned char byte2 = (mut1&0xFF)^(mut2&0xFF); if( !byte1 ) byte1 = 1; if( !byte2 ) byte2 = 1; byte1seeds[0] = byte1^byte2; byte1seeds[1] = byte2; byte1seeds[2] = byte1; }
现在C#代码:
我已经改变了函数GenerateValue.它没有指针作为参数,它有一个ulong参数.
要调用它并更改两个值,我使用:
ulong mut1 = GenerateValue(mut);
mut = mut1;
以下是已翻译的函数(有问题的行标有"// error");
//Global var: public static byte[] byte1seeds = new byte[3]; public static ulong GenerateValue(ulong val) { for( int i = 0; i < 32; i++ ) val = (((((((((((val >> 2)^val) >> 2)^val) >> 1)^val) >> 1)^val) >> 1)^val)&1)|((((val&1) << 31)|(val >> 1))&0xFFFFFFFE); return val ; } public static void SetupCountByte( uint seed ) { if( seed == 0 ) seed = 0x9ABFB3B6; ulong mut = seed; ulong mut1 = GenerateValue(mut); mut = mut1; ulong mut2 = GenerateValue(mut); mut = mut2; ulong mut3 = GenerateValue(mut); mut = mut3; mut = GenerateValue(mut); byte byte1 = (mut & 0xFF) ^ (mut3 & 0xFF); //error byte byte2 = (mut1 & 0xFF) ^ (mut2 & 0xFF); //error if( byte1 != 0 ) byte1 = 1; if( byte2 != 0 ) byte2 = 1; byte1seeds[0] = byte1^byte2; //error byte1seeds[1] = byte2; byte1seeds[2] = byte1; }
错误是:
无法将类型'ulong'隐式转换为'byte'.存在显式转换(您是否错过了演员?)
编辑:有问题的第3行的错误是:
无法将类型'int'隐式转换为'byte'.存在显式转换(您是否错过了演员?)
问题出在这里:如何解决这些错误?
提前致谢!
添加一个(byte)
来投射它.由于您可能会失去精度,您必须告诉编译器该值将适合一个字节,即
byte byte1 = (byte)((mut & 0xFF) ^ (mut3 & 0xFF)); byte byte2 = (byte)((mut1 & 0xFF) ^ (mut2 & 0xFF));