C#中:不能从ULONG转换为字节 [英] C#: Cannot convert from ulong to byte

查看:320
本文介绍了C#中:不能从ULONG转换为字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

奇怪我怎么能做到这一点在C ++中,但不是在C#。

Strange how I can do it in C++,but not in C#.

为了明确这一点,我会粘贴两个功能在C ++中,然后在C#并标志着C#代码有问题的行以注释//错误。
什么这两个函数的作用是编码参数,然后将它添加到名为byte1seeds全局变量。

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.

这是在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.Instead,它有一个ULONG参数。

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:


  1. ULONG MUT1 = GenerateValue(MUT);

  2. 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:

无法隐式转换类型'ULONG'到'字节'。一个显式转换存在(是否缺少强制转换?)

Cannot implicitly convert type 'ulong' to 'byte'. An explicit conversion exists (are you missing a cast?)

编辑:在有问题的第3行的错误是:

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?

在此先感谢!

推荐答案

添加(字节)投它。正如你可能会失去精度,你一定要告诉该值将放入一个字节的编译器,即

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.

byte byte1 = (byte)((mut & 0xFF) ^ (mut3 & 0xFF));
byte byte2 = (byte)((mut1 & 0xFF) ^ (mut2 & 0xFF));

这篇关于C#中:不能从ULONG转换为字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆