C#:无法从 ulong 转换为 byte
-
22-08-2019 - |
题
奇怪的是我可以在 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));
其他提示
您可能会丢失信息。编译器不允许这种操作,除非你明确告诉他这样做。所以尝试这样的事情:
byte result = ((byte)mut & 0xFF) ^ ((byte)mut3 & 0xFF);
这样,所有变量都会被显式转换,结果将是一个字节。或者你可以这样做:
byte result = (byte)((mut & 0xFF) ^ (mut3 & 0xFF));
症状
以下代码在 C++ 中编译,但被 C# 编译器拒绝,并在第三行报告类型不兼容。
ulong mut = 5;
ulong mut3 = 6;
byte foo = (mut & 0xFF) ^ (mut3 & 0xFF);
解释
表达方式 (mut & 0xFF) ^ (mut3 & 0xFF)
属于类型 ulong
并且不能分配给类型的变量 byte
.
变量 mut
是一个 ulong
. 。所有超载 &
要求操作数类型对称,所以在表达式中 (mut & 0xFF)
, , 价值 0xFF
被提升为 ulong
, ,运算结果的类型为 ulong
.
虽然类似的过程也给出了第二个子表达式的类型 ulong
, ,这是偶然的,因为在更大的表达式中 A ^ B
, ,事实上表达式 A
有类型 ulong
会引起表达 B
被提升。
因此,表达式 (mut & 0xFF) ^ (mut3 & 0xFF)
属于类型 ulong
并且需要显式转换才能将其分配给类型的变量 byte
.
解决方案
在赋值之前显式地对整个表达式进行类型转换。
评论
人们关闭警告而不是考虑它们,因为大多数 C+ 库都充满了缺陷。如果你重新打开警告,你会收到这么多警告,尝试费力地浏览它们是没有用的,即使在混乱的某个地方会有一个注释,说明“需要潜在有损的隐式类型转换”。
如果您阅读 C# 语言规范,特别是运算符,您将学到很多有用的东西。例如,此代码将失败:
byte b = 0xF0 | 0x0E; //b should contain 0xFE
但这些都会成功:
byte b1 = (byte)(0xF0 | 0x0E); //typecast fixes it
byte b2 = 0xF0;
b2 |= 0x0E; //reflexive operator typed by target variable
“存在显式转换”表示您需要进行显式转换。在这种情况下,会是这样的:
byte byte1 = (byte) ( (mut & 0xFF) ^ (mut3 & 0xFF) );
byte byte2 = (byte) ( (mut1 & 0xFF) ^ (mut2 & 0xFF) );
有一个 MSDN 知识库文章 这深入解释了明确的沮丧的必要性。错误消息中的“存在显式转换”一词旨在提示您必须使用强制转换显式转换数据类型。在您的具体情况下,它看起来像这样:
byte byte1 = (byte) ( (mut & 0xFF) ^ (mut3 & 0xFF) );