日期:2014-05-19  浏览次数:20710 次

急....C语言代码转成java代码
C/C++ code

void GetChkSum(Int len, PSTR buf, PSTR res)
{
    memset(res, 0, 8); //IIN格式的校验码为8字节
    for(int i=0; i<len; i+=4)
    {
        res[0]^=(buf+i)[0];
        res[1]^=(buf+i)[1];
        res[2]^=(buf+i)[2];
        res[3]^=(buf+i)[3];
    };
    res[0]=~res[0];
    res[1]=~res[1];
    res[2]=~res[2];
    res[3]=~res[3];
    // 将16进制数扩展为对应字符数组(如0xE8--->"E8")
    for(i = 7; i >= 0; i --)
    {
        if ( i % 2 ) // 低4位所代表16进制表字符扩展为一个字节
        {
            res[i] = (res[i/2] & 0x0F) + '0';
            if ( res[i] > '9' )
            {
                res[i] = res[i] + 'A' - '0' - 10;
            }
        }
        else //高4位所代表16进制表字符扩展为一个字节
        {
            res[i] = ((res[i/2] >> 4) & 0x0F) + '0';
            if ( res[i] > '9' )
            {
                res[i] = res[i] + 'A' - '0' - 10;
            }
        }
    }
}





以上代码用java代码如何编写。。。。

在此感谢大家了。。

------解决方案--------------------
PSTR 这是什么类型?

出来这个+ 'A' - '0',
其他基本一样能在java上运行
------解决方案--------------------
Java code
//void GetChkSum(Int len, PSTR buf, PSTR res)
void GetChkSum(int len, char[] buf, char[] res)
{   //不知道这里该对应char数组还是byte数组,如果是对应byte数组,LZ自己把char改成byte就可以了
    //memset(res, 0, 8); //IIN格式的校验码为8字节
    Arrays.fill(res, 0, 8, (char)0);
    for(int i=0; i<len; i+=4)
    {
        //res[0]^=(buf+i)[0];
        //res[1]^=(buf+i)[1];
        //res[2]^=(buf+i)[2];
        //res[3]^=(buf+i)[3];
        res[0]^=buf[i+0];
        res[1]^=buf[i+1];
        res[2]^=buf[i+2];
        res[3]^=buf[i+3];

    };
    res[0]=~res[0];
    res[1]=~res[1];
    res[2]=~res[2];
    res[3]=~res[3];
    // 将16进制数扩展为对应字符数组(如0xE8--->"E8")
    for(i = 7; i >= 0; i --)
    {
        if ( i % 2 ) // 低4位所代表16进制表字符扩展为一个字节
        {
            res[i] = (char)((res[i/2] & 0x0F) + '0');
            if ( res[i] > '9' )
            {
                res[i] = (char)(res[i] + 'A' - '0' - 10);
            }
        }
        else //高4位所代表16进制表字符扩展为一个字节
        {
            res[i] = (char)(((res[i/2] >> 4) & 0x0F) + '0');
            if ( res[i] > '9' )
            {
                res[i] = (char)(res[i] + 'A' - '0' - 10);
            }
        }
    }
}