请把这段C#代码转换为Delphi ( 积分: 200 )

  • 主题发起人 主题发起人 poppy
  • 开始时间 开始时间
P

poppy

Unregistered / Unconfirmed
GUEST, unregistred user!
代码:
using System;
namespace PRCrack
{
    /// <summary>
    /// Google PageRank的Checksum算法。
    /// 
    /// 
    /// 
    /// </summary>
    class CheckSum
    {
        public CheckSum()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        uint GOOGLE_MAGIC = 0xE6359A60;
        private uint zeroFill(uint a, int b)
        {
            checked
            {
                uint z = 0x80000000;
                if (Convert.ToBoolean(z &amp;
a))
                {
                    a = (a >> 1);
                    a &amp;= (~z);
                    a |= 0x40000000;
                    a = (a >> (b - 1));
                }
                else
                {
                    a = (a >> b);
                }
            }
            return a;
        }
        private uint[] mix(uint a, uint b, uint c)
        {
            a -= b;
a -= c;
a ^= (uint)(zeroFill(c, 13));
            b -= c;
b -= a;
b ^= (uint)(a << 8);
            c -= a;
c -= b;
c ^= (uint)(zeroFill(b, 13));
            a -= b;
a -= c;
a ^= (uint)(zeroFill(c, 12));
            b -= c;
b -= a;
b ^= (uint)(a << 16);
            c -= a;
c -= b;
c ^= (uint)(zeroFill(b, 5));
            a -= b;
a -= c;
a ^= (uint)(zeroFill(c, 3));
            b -= c;
b -= a;
b ^= (uint)(a << 10);
            c -= a;
c -= b;
c ^= (uint)(zeroFill(b, 15));
            return new uint[3] { a, b, c };
        }
        private uint GoogleCH(uint[] url, uint length, uint init)
        {
            if (length == 0)
            {
                length = (uint)url.Length;
            }
            uint a, b;
            a = b = 0x9E3779B9;
            uint c = init;
            int k = 0;
            uint len = length;
            uint[] m_mix = new uint[3];
            while (len >= 12)
            {
                a += (uint)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k + 3] << 24));
                b += (uint)(url[k + 4] + (url[k + 5] << 8) + (url[k + 6] << 16) + (url[k + 7] << 24));
                c += (uint)(url[k + 8] + (url[k + 9] << 8) + (url[k + 10] << 16) + (url[k + 11] << 24));
                m_mix = mix(a, b, c);
                a = m_mix[0];
b = m_mix[1];
c = m_mix[2];
                k += 12;
                len -= 12;
            }
            c += length;
            switch (len)              /* all the case statements fall through */
            {
                case 11:
                    {
                        c += (uint)(url[k + 10] << 24);
                        c += (uint)(url[k + 9] << 16);
                        c += (uint)(url[k + 8] << 8);
                        b += (uint)(url[k + 7] << 24);
                        b += (uint)(url[k + 6] << 16);
                        b += (uint)(url[k + 5] << 8);
                        b += (uint)(url[k + 4]);
                        a += (uint)(url[k + 3] << 24);
                        a += (uint)(url[k + 2] << 16);
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                case 10:
                    {
                        c += (uint)(url[k + 9] << 16);
                        c += (uint)(url[k + 8] << 8);
                        b += (uint)(url[k + 7] << 24);
                        b += (uint)(url[k + 6] << 16);
                        b += (uint)(url[k + 5] << 8);
                        b += (uint)(url[k + 4]);
                        a += (uint)(url[k + 3] << 24);
                        a += (uint)(url[k + 2] << 16);
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                case 9:
                    {
                        c += (uint)(url[k + 8] << 8);
                        b += (uint)(url[k + 7] << 24);
                        b += (uint)(url[k + 6] << 16);
                        b += (uint)(url[k + 5] << 8);
                        b += (uint)(url[k + 4]);
                        a += (uint)(url[k + 3] << 24);
                        a += (uint)(url[k + 2] << 16);
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                /* the first byte of c is reserved for the length */
                case 8:
                    {
                        b += (uint)(url[k + 7] << 24);
                        b += (uint)(url[k + 6] << 16);
                        b += (uint)(url[k + 5] << 8);
                        b += (uint)(url[k + 4]);
                        a += (uint)(url[k + 3] << 24);
                        a += (uint)(url[k + 2] << 16);
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                case 7:
                    {
                        b += (uint)(url[k + 6] << 16);
                        b += (uint)(url[k + 5] << 8);
                        b += (uint)(url[k + 4]);
                        a += (uint)(url[k + 3] << 24);
                        a += (uint)(url[k + 2] << 16);
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                case 6:
                    {
                        b += (uint)(url[k + 5] << 8);
                        b += (uint)(url[k + 4]);
                        a += (uint)(url[k + 3] << 24);
                        a += (uint)(url[k + 2] << 16);
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                case 5:
                    {
                        b += (uint)(url[k + 4]);
                        a += (uint)(url[k + 3] << 24);
                        a += (uint)(url[k + 2] << 16);
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                case 4:
                    {
                        a += (uint)(url[k + 3] << 24);
                        a += (uint)(url[k + 2] << 16);
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                case 3:
                    {
                        a += (uint)(url[k + 2] << 16);
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                case 2:
                    {
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                case 1:
                    {
                        a += (uint)(url[k + 0]);
                        break;
                    }
                /* case 0: nothing left to add */
            }
            m_mix = mix(a, b, c);
            /*-------------------------------------------- report the result */
            return m_mix[2];
        }
        private uint GoogleCH(string url, uint length)
        {
            uint[] m_urluint = new uint[url.Length];
            for (int i = 0;
i < url.Length;
i++)
            {
                m_urluint[i] = url[i];
            }
            return GoogleCH(m_urluint, length, GOOGLE_MAGIC);
        }
        private uint GoogleCH(string sURL)
        {
            return GoogleCH(sURL, 0);
        }
        private uint GoogleCH(uint[] url, uint length)
        {
            return GoogleCH(url, length, GOOGLE_MAGIC);
        }
        private uint[] c32to8bit(uint[] arr32)
        {
            uint[] arr8 = new uint[arr32.GetLength(0) * 4 + 3];
            for (int i = 0;
i < arr32.GetLength(0);
i++)
            {
                for (int bitOrder = i * 4;
bitOrder <= i * 4 + 3;
bitOrder++)
                {
                    arr8[bitOrder] = arr32[i] &amp;
255;
                    arr32[i] = zeroFill(arr32[i], 8);
                }
            }
            return arr8;
        }
        //新算法,ToolBar 版本>>=2.0.114
        public string CalculateChecksum(string sURL)
        {
            uint ch = GoogleCH("info:" + sURL);
            ch = (((ch / 7) << 2) | (((uint)(ch % 13)) &amp;
7));
            uint[] prbuf = new uint[20];
            prbuf[0] = ch;
            for (int i = 1;
i < 20;
i++)
            {
                prbuf[i] = prbuf[i - 1] - 9;
            }
            ch = GoogleCH(c32to8bit(prbuf), 80);
            return string.Format("6{0}", ch);
        }
        //老算法,ToolBar 版本<2.0.114
        public string CalculateChecksumOld(string sURL)
        {
            uint ch = GoogleCH("info:" + sURL);
            string CalculateChecksum = "6" + Convert.ToString((ch));
            return CalculateChecksum;
        }
    }
}
 
如分不够可以再加,谢谢
 
太长了吧。。。
说下转换规律自己转了
1、所有的{改成begin
,}改成end
2、所有的方法改成过程或函数
方法里有return xxx的就是函数,没有的就是过程
如public string CalculateChecksumOLd(string sURL)
改为 Function CalculateChecksumOld(sURL:string):string
3、C#可以在用的时候才定义,delphi要先定义,包括函数和过程
4、for (int i = 0;
i < url.Length;
i++)
相当于 for i:=0 to length(url)do
5、/* 。。。。*/也是注释
 
checked
是什么?
 
晕.......................silver888你这不是有说等于没说吗[V]
 
checked是int数据溢出检查
 
各位,或是帮我把这个封装成Com也行,我通过delphi直接调用算了
这段程序好多位操作,想转好烦
 
我的邮箱:sothis09@yahoo.com.cn
 
delphi里面好像没那checked
 
***************************
让ロロロ来帮你!!!
***************************
1,把下面的代码保存为CheckSum.pas
unit CheckSum;
interface
uses SysUtils;
const GOOGLE_MAGIC = $E6359A60;
type
TMixArray = array of LongWord;
{
//Private Methold:Does not need to be declared
function zeroFill(a:LongWord;
b: Integer): LongWord;
function mix(a:LongWord;
b:LongWord;
c:LongWord):TMixArray;
function GoogleCH(url:TMixArray;
length:LongWord;
init:LongWord):LongWord;overload;
function GoogleCH(url:string;
length:LongWord):LongWord;overload;
function GoogleCH(sURL:string):LongWord;overload;
function GoogleCH(url:TMixArray;
length:LongWord):LongWord;overload;
function c32to8bit(arr32:TMixArray):TMixArray;
}
function CalculateChecksum(sURL:string):string;
function CalculateChecksumOld(sURL:string):string;
implementation

function zeroFill(a:LongWord;
b: Integer): LongWord;
var
z:LongWord;
begin
begin
z := $80000000;
if ((z and a)<>0) then
begin
a := (a shr 1);
a := a and (not z);
a := a or $40000000;
a := (a shr (b - 1));
end
else
begin
a := (a shr b);
end;
end;
result:=a;
end;

function mix(a:LongWord;
b:LongWord;
c:LongWord):TMixArray;
var
mixArray:TMixArray;
begin
//
a := a-b;
a := a-c;
a := a xor (zeroFill(c, 13));
//
b := b - c;
b := b - a;
b := b xor (a shl 8);
//
c := c - a;
c := c - b;
c := c xor (zeroFill(b, 13));
//
a := a - b;
a := a - c;
a := a xor (zeroFill(c, 12));
//
b := b - c;
b := b - a;
b := b xor (a shl 16);
//
c := c - a;
c := c - b;
c := c xor (zeroFill(b, 5));
//
a := a - b;
a := a - c;
a := a xor (zeroFill(c, 3));
//
b := b - c;
b := b - a;
b := b xor (a shl 10);
c := c - a;
c := c - b;
c := c xor (zeroFill(b, 15));
setLength(mixArray, 3);
mixArray[0]:=a;
mixArray[1]:=b;
mixArray[2]:=c;
result := mixArray;
end;

function GoogleCH(url:TMixArray;
length:LongWord;
init:LongWord):LongWord;overload;
var
a,b,c,len:LongWord;
k:Integer;
m_mix:TMixArray;
begin
if (length = 0) then
begin
length := High(url) - Low(url) + 1;
end;
a := $9E3779B9;
b := a;
c := init;
k := 0;
len := length;
setLength(m_mix, 3);
while (len >= 12)do
begin
a := a + (url[k+0] + (url[k+1] shl 8) + (url[k+2] shl 16) + (url[k+3] shl 24));
b := b + (url[k+4] + (url[k+5] shl 8) + (url[k+6] shl 16) + (url[k+7] shl 24));
c := c + (url[k+8] + (url[k+9] shl 8) + (url[k+10] shl 16) + (url[k+11] shl 24));
m_mix := mix(a, b, c);
a := m_mix[0];
b := m_mix[1];
c := m_mix[2];
k := k + 12;
len := len - 12;
end;

c := c + length;
case len of { all the case statements fall through }
11:
begin
c := c + (url[k + 10] shl 24);
c := c + (url[k + 9] shl 16);
c := c + (url[k + 8] shl 8);
b := b + (url[k + 7] shl 24);
b := b + (url[k + 6] shl 16);
b := b + (url[k + 5] shl 8);
b := b + (url[k + 4]);
a := a + (url[k + 3] shl 24);
a := a + (url[k + 2] shl 16);
a := a + (url[k + 1] shl 8);
a := a + (url[k + 0]);
end;
10:
begin
c := c + (url[k + 9] shl 16);
c := c + (url[k + 8] shl 8);
b := b + (url[k + 7] shl 24);
b := b + (url[k + 6] shl 16);
b := b + (url[k + 5] shl 8);
b := b + (url[k + 4]);
a := a + (url[k + 3] shl 24);
a := a + (url[k + 2] shl 16);
a := a + (url[k + 1] shl 8);
a := a + (url[k + 0]);
end ;
9:
begin
c := c + (url[k + 8] shl 8);
b := b + (url[k + 7] shl 24);
b := b + (url[k + 6] shl 16);
b := b + (url[k + 5] shl 8);
b := b + (url[k + 4]);
a := a + (url[k + 3] shl 24);
a := a + (url[k + 2] shl 16);
a := a + (url[k + 1] shl 8);
a := a + (url[k + 0]);
end;
{the first byte of c is reserved for the length }
8:
begin
b := b + (url[k + 7] shl 24);
b := b + (url[k + 6] shl 16);
b := b + (url[k + 5] shl 8);
b := b + (url[k + 4]);
a := a + (url[k + 3] shl 24);
a := a + (url[k + 2] shl 16);
a := a + (url[k + 1] shl 8);
a := a + (url[k + 0]);
end;
7:
begin
b := b + (url[k + 6] shl 16);
b := b + (url[k + 5] shl 8);
b := b + (url[k + 4]);
a := a + (url[k + 3] shl 24);
a := a + (url[k + 2] shl 16);
a := a + (url[k + 1] shl 8);
a := a + (url[k + 0]);
end;
6:
begin
b := b + (url[k + 5] shl 8);
b := b + (url[k + 4]);
a := a + (url[k + 3] shl 24);
a := a + (url[k + 2] shl 16);
a := a + (url[k + 1] shl 8);
a := a + (url[k + 0]);
end;
5:
begin
b := b + (url[k + 4]);
a := a + (url[k + 3] shl 24);
a := a + (url[k + 2] shl 16);
a := a + (url[k + 1] shl 8);
a := a + (url[k + 0]);
end;
4:
begin
a := a + (url[k + 3] shl 24);
a := a + (url[k + 2] shl 16);
a := a + (url[k + 1] shl 8);
a := a + (url[k + 0]);
end;
3:
begin
a := a + (url[k + 2] shl 16);
a := a + (url[k + 1] shl 8);
a := a + (url[k + 0]);
end;
2:
begin
a := a + (url[k + 1] shl 8);
a := a + (url[k + 0]);
end;
1:
begin
a := a + (url[k + 0]);
end;
{ 0: nothing left to add }
end;
m_mix := mix(a, b, c);
result :=m_mix[2];
end;

function GoogleCH(url:string;
length:LongWord):LongWord;overload;
var
m_urluint:TMixArray;
i:Integer;
count:Integer;
begin
count:= System.length(url);
SetLength(m_urluint, count);
for i := 0 to count-1do
begin
m_urluint := Ord(url[i+1]);
end;
result := GoogleCH(m_urluint, length, GOOGLE_MAGIC);
end;

function GoogleCH(sURL:string):LongWord;overload;
begin
result := GoogleCH(sURL, 0);
end;

function GoogleCH(url:TMixArray;
length:LongWord):LongWord;overload;
begin
result := GoogleCH(url, length, GOOGLE_MAGIC);
end;

function c32to8bit(arr32:TMixArray):TMixArray;
var
arr8:TMixArray;
count:Integer;
i, bitOrder:Integer;
begin
count:= Length(arr32);
SetLength(arr8, count*4 + 3);
for i := 0 to count -1do
begin
for bitOrder := i * 4 to i * 4 + 3 do
begin
arr8[bitOrder] := arr32 and 255;
arr32 := zeroFill(arr32, 8);
end;
end;
result := arr8;
end;

//New ToolBar Ver >=2.0.114
function CalculateChecksum(sURL:string):string;
var
ch:LongWord;
prbuf:TMixArray;
i:Integer;
begin
ch := GoogleCH('info:' + sURL);
ch := (((ch div 7) shl 2) or (((ch mod 13)) and 7));
SetLength(prbuf, 20);
prbuf[0] := ch;
for i := 1 to 20 -1do
begin
prbuf := prbuf[i - 1] - 9;
end;
ch := GoogleCH(c32to8bit(prbuf), 80);
result := Format('6%d', [ch]);
end;

//Old ToolBar Ver<2.0.114
function CalculateChecksumOld(sURL:string):string;
var
ch:LongWord;
begin
ch := GoogleCH('info:' + sURL);
result := '6' + IntToStr(ch);
end;

end.


2,在调用的窗体的uses里面,加入上面的unit。
uses 。。。。`,CheckSum;
3,使用方法

//新版本的呼叫
ShowMessage(CalculateChecksum('http://192.168.1.2'));
//旧版本的呼叫
ShowMessage(CalculateChecksumOld('http://192.168.1.2'));

4,说明完毕

***************************
有问题么?让ロロロ来帮你吧
***************************
 
ロロロ是谁?
 
姝e湪鐮旂┒C#涓?紝鎷挎潵鐮旂┒鐮旂┒銆傘
 
后退
顶部