日期:2014-05-18  浏览次数:20842 次

帮忙将C代码转成C#,急呀!

C 代码如下,小弟自己手工转换后很多错误,请大家帮帮忙,非常感谢!

C/C++ code



//反Base64数组,可以优化,变成全局,一次生成
char ReverseBase[256];


//函数所用到的变量初始化
int Funs_Gps_Init()
{
    Base64Init();
    return 1;
}

//转义函数
int Transfer7D(char *NewBuf,const char *p_OldBuf,int OldLen)
{
    unsigned char* OldBuf=(unsigned char*)p_OldBuf;
    int Offset=0;
    for(int i=0;i<OldLen;i++)
    {
        if(OldBuf[i] ==0x7d || OldBuf[i] ==0x7e || OldBuf[i] ==0x7f)
        {
            //需要转义
            NewBuf[Offset++]=0x7D;
            NewBuf[Offset++]=OldBuf[i] ^ 0x20;

        }else
        {
            NewBuf[Offset++]=OldBuf[i];
        }
    }
    return Offset;
}

//翻转义
int Restore7D(const char* pBuff,int BuffLen,char* Dest)
{
    unsigned char *Src=(unsigned char *)pBuff;
    int i=0;
    int iDest=0;
    for(;i<BuffLen;++i,++iDest)
    {
        if(*(Src+i)==0x7D)
        {
            ++i;
            *(Dest+iDest)=*(Src+i) ^ 0x20;
        }else
        {
            *(Dest+iDest)=*(Src+i);
        }
    }

    return iDest;
}


//在反Base64时的数组初始化
int Base64Init()
{
    char Base[]= 
    {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', 
    'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f', 
    'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v', 
    'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/','='};

    char i=0;
    for(;i<sizeof(Base)-1;++i)
    {
        ReverseBase[Base[i]]=i;
    }
    return 1;
}
//一种编码方式
int ToBase64(const char * Buf,int Len,char * Base64Buf)
{
    char Base[]= 
    {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', 
    'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f', 
    'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v', 
    'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/','='};
    //求出倍数
    int Multiple3 = Len/3;
    int Offset=0;
    char OldTemp[3];
    for(int i=0;i<Multiple3;i++)
    {
        memcpy(OldTemp,Buf + 3*i,3);
        Base64Buf[Offset++]= Base[((OldTemp[0]>>2) & 0x3F)];
        Base64Buf[Offset++]= Base[(((OldTemp[0] & 0x03 )<<4) | (OldTemp[1]>>4 & 0x0F))];
        Base64Buf[Offset++]= Base[(((OldTemp[1] & 0x0F)<<2 ) | (OldTemp[2]>>6 & 0x03))] ;
        Base64Buf[Offset++]= Base[((OldTemp[2]) & 0x3F)];
    }
    if((Len%3)==0)
    {
    }
    else if((Len % 3)==1)
    {
        memcpy(OldTemp,Buf + Multiple3 * 3 ,1);
        //用来补
        //OldTemp[1] = OldTemp[2] = '=';
        Base64Buf[Offset++]= Base[((OldTemp[0]>>2) & 0x3F)];
        Base64Buf[Offset++]= Base[(((OldTemp[0] & 0x03 )<<4) )];
        Base64Buf[Offset++]= '=';
        Base64Buf[Offset++]= '=';
    }
    else
    {
        memcpy(OldTemp,Buf +  Multiple3 * 3,2);
        //用来补
        //OldTemp[2] = '=';
        Base64Buf[Offset++]= Base[((OldTemp[0]>>2) & 0x3F)];
        Base64Buf[Offset++]= Base[(((OldTemp[0] & 0x03 )<<4) | (OldTemp[1]>>4 & 0x0F))];
        Base64Buf[Offset++]= Base[(((OldTemp[1] & 0x0F)<<2 ) )] ;
        Base64Buf[Offset++]= '=';
    }
    return Offset;
}


//反Base64编码
int FromBase64(const char* Buf,int Len,char* HexBuf)
{
    if(Len % 4 !=0)
    {
        return 0;
    }

    int EqualCount=0;       //数据的最后等号个数
    //最后的数据
    if(Buf[Len-1]=='=')
    {
        EqualCount++;
    }
    if(Buf[Len-2]=='=')
    {
        EqualCount++;
    }


    char tmp[4];
    int i=0;
    int offset=0;
    
    while(offset < Len)
    {
        //提取出来
        tmp[0]=ReverseBase[Buf[offset++]];
        tmp[1]=ReverseBase[Buf[offset++]];
        tmp[2]=ReverseBase[Buf[offset++]];
        tmp[3]=ReverseBase[Buf[offset++]];

        

        //3f=00111111  0x30=00110000
        HexBuf[i++]=((tmp[0] & 0x3F )<<2) | ((tmp[1] & 0x3