当前位置:  开发笔记 > 编程语言 > 正文

如何在C#中声明LARGE_INTEGER

如何解决《如何在C#中声明LARGE_INTEGER》经验,为你挑选了2个好方法。

下面的代码(在C++中)是我尝试转换为C#

DWORD Func_X_4(DWORD arg1, DWORD arg2, DWORD arg3)
{
LARGE_INTEGER result = {1, 0};
LARGE_INTEGER temp1 = {0};
LARGE_INTEGER temp2 = {0};
LARGE_INTEGER temp3 = {0};
LARGE_INTEGER temp4 = {0};
for(int x = 0; x < 32; ++x)
{
    if(arg2 & 1)
    {
        temp1.LowPart = arg3;
        temp1.HighPart = 0;
        temp2.QuadPart = temp1.QuadPart * result.QuadPart;
        temp3.LowPart = arg1;
        temp3.HighPart = 0;
        temp4.QuadPart = temp2.QuadPart % temp3.QuadPart;
        result.QuadPart = temp4.QuadPart;
    }
    arg2 >>= 1;
    temp1.LowPart = arg3;
    temp1.HighPart = 0;
    temp1.QuadPart *= temp1.QuadPart;
    temp2.LowPart = arg1;
    temp2.HighPart = 0;
    temp3.QuadPart = temp1.QuadPart % temp2.QuadPart;
    arg3 = temp3.LowPart;
    if(!arg2)
        break;
}
return result.LowPart;
}

在这里,我尝试翻译代码,但它太乱了,我之前从未使用过大整数.

结构体:

public struct LARGE_INTEGER
{
    UInt32 LowPart;
    Int32 HighPart;
    Int32 QuadPart;
}

翻译功能:

    public Int32 Func_X_4(Int32 arg1, Int32 arg2, Int32 arg3)
    {
    LARGE_INTEGER result = {1, 0}; //this and the four below,are they correct?
    LARGE_INTEGER temp1 = {0, 0};
    LARGE_INTEGER temp2 = {0, 0};
    LARGE_INTEGER temp3 = {0, 0};
    LARGE_INTEGER temp4 = {0, 0};
    for(int x = 0; x < 32; ++x)
    {
        if(arg2 & 1==0) //correct?
        {
            temp1.LowPart = arg3;
            temp1.HighPart = 0;
            temp2.QuadPart = temp1.QuadPart * result.QuadPart;
            temp3.LowPart = arg1;
            temp3.HighPart = 0;
            temp4.QuadPart = temp2.QuadPart % temp3.QuadPart;
            result.QuadPart = temp4.QuadPart;
        }
        arg2 >>= 1;
        temp1.LowPart = arg3;
        temp1.HighPart = 0;
        temp1.QuadPart *= temp1.QuadPart;
        temp2.LowPart = arg1;
        temp2.HighPart = 0;
        temp3.QuadPart = temp1.QuadPart % temp2.QuadPart;
        arg3 = temp3.LowPart;
        if(arg2 != 0) //correct?
        break;
    }
    return result.LowPart;
}

问题:第一个问题是我没有在C#中找到一个LARGE_INTEGER类型变量,所以我创建了一个结构,我想知道是否确实存在.至于第二个问题,功能不对,没有用.

任何有关该特定问题的帮助都将非常感谢!先感谢您.



1> Jim Mischel..:

LARGE_INTEGER结构的直接转换是:

[StructLayout(LayoutKind.Explicit, Size=8)]
struct LARGE_INTEGER
{
    [FieldOffset(0)]public Int64 QuadPart;
    [FieldOffset(0)]public UInt32 LowPart;
    [FieldOffset(4)]public Int32 HighPart;
}

它就像C中的联合,其中QuadPart一个8字节值,LowPart占用前4个字节并HighPart占用高4个字节.


使用LayoutKind.Explicit而不是LayoutKind.Absolute

2> eglasius..:

它的Int64.基于http://msdn.microsoft.com/en-us/library/aa383713.aspx,它是一个64位有符号整数.

推荐阅读
落单鸟人
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有