不使用bigint类进行按位运算? [英] bitwise operation without the use of a bigint class?

查看:85
本文介绍了不使用bigint类进行按位运算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.

我知道我曾经问过一个类似的问题,但是我仍然面临着类似的问题,并且我设法将我的问题浓缩了起来,希望可以更好地理解它?

我有以下功能/代码:

Hello.

I know I have asked a simular question before, but I am still facing problems with it, and I have managed to condense my problem so that, hopefully, it is a little better to understand?

I have the following function/code:

private void button1_Click(object sender, EventArgs e)
        {

            // Raw license, fixed bytes compiled from productID, product Version, License Data, expiery and 2-3 random bytes.
            byte[] rawLicense = {0x00, 0x00, 0xA3, 0xE1};


            int  i = rawLicense[3] + (rawLicense[2] << 8) + (rawLicense[1] << 16) + (rawLicense[0] << 24);

        }




它采用十六进制值00 00 A3 E1并使用移位来形成该十六进制值的int表示,即:41953


现在,我该如何编辑上面的函数以允许我使用更大长度的字节数组,这将产生一个如下所示的int:

45434-59685-45565-59685-56923-49565

我宁愿不使用bigint类,而且我已经看到了与上述类似的代码来执行此操作,从而生成了一个表示较大int的字符串,或者至少是一个类似于上述的数字字符串,当然还有一个方法将字符串反转为字节数组.

非常感谢,
亲切的问候,
Stephen




This takes the hex value 00 00 A3 E1 and uses bitshifting to form the int representation of that hex value, which is: 41953


Now, how can I edit the above function to allow me to work with a byte array of a bigger length, that would produce an int like the one below:

45434-59685-45565-59685-56923-49565

I would prefer not to use a bigint class, and I have seen code simular to the above that does this, producing a string that represents a larger int, or at least a string of digits like the above, and, of course, a method to reverse the string into a byte array.

Thank you very much,
Kind Regards,
Stephen

推荐答案

为什么不将上面按钮中的代码封装为方法:


Why don''t you encapsulate the code in the button above into a method:


int GetIntFromByteArr(byte[] rawLicense)
{
  int  i = rawLicense[3] + (rawLicense[2] << 8) + (rawLicense[1] << 16)     + (rawLicense[0] << 24);
  return j;
}



并多次调用?



And call it multiple times?

int el1 = GetIntFromByteArr(new byte[]{0x00, 0x00, 0xA3, 0xE1});
int el2 = GetIntFromByteArr(new byte[]{0x00, 0x10, 0x00, 0xB1});
// ....



然后,您只需串联这些整数即可.

您当然可以通过将数组传递给方法和表示要转换的开始/结束位置的索引的方式来对此进行优化.

希望这会有所帮助,
Alex



Then you would just concatenate those integers.

You could of course optimize this in such a way that you are passing the array to the method and the indexes that represent the start/end position to convert.

Hope this helps,
Alex


好的,让我们再试一次:)

尝试这段代码:

All right, let''s try this again :)

Try this piece of code:

class Program
{
    static void Main(string[] args)
    {
        byte[] rawLicense = { 0x00, 0x00, 0xA3, 0xE1, 0xA5, 0xB6 };

        int shift = 0;
        long result = 0;
        for (int i = rawLicense.Length - 1; i >=0 ; i--, shift += 8)
        {
            long value = (long)rawLicense[i] << shift;
            result += value;
        }

        Console.WriteLine(result);
    }
}



您仍然受制于长时间不会溢出的情况,但请注意这一点.



You are still limited to what a long can hold without overflowing though so be careful about that.


这篇关于不使用bigint类进行按位运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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