Strange how I can do it in C++,but not in C#.
To make it clear,i’ll paste the two functions in C++ and then in C# and mark the problematic lines in the C# code with a comment ‘//error’. What the two function does is encoding the parameter and then add it in to a global variable named byte1seeds.
These are the functions in 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; }
Now the C# code:
I’ve changed the function GenerateValue.Instead of having a pointer as a parameter, it has a ulong parameter.
To call it and change both values i use:
- ulong mut1 = GenerateValue(mut);
- mut = mut1;
Here are the translated functions(the problematic lines are marked with ‘//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; }
The error is:
Cannot implicitly convert type ‘ulong’ to ‘byte’. An explicit conversion exists (are you missing a cast?)
edit:the error at problematic line 3 is:
Cannot implicitly convert type ‘int’ to ‘byte’. An explicit conversion exists (are you missing a cast?)
Here comes the question: How to solve those errors?
Thanks in advance!
Add a
(byte)to cast it. As you could lose precision, you have to tell the compiler that the value will fit into a byte, i.e.