Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将小型C#校验和程序转换为Java_C#_Java_Checksum - Fatal编程技术网

将小型C#校验和程序转换为Java

将小型C#校验和程序转换为Java,c#,java,checksum,C#,Java,Checksum,我正试图为遥控飞机建立一个简单的地面控制站。我几乎完成了,但是我在校验和计算方面遇到了很多麻烦。我知道Java和C#的数据类型是不同的。我试图解释这一点,但我不确定我是否成功。该程序采用CRC-16-CCITT方法 这是我的端口: public int crc_accumulate(int b, int crc) { int ch = (b ^ (crc & 0x00ff)); ch = (ch ^ (ch << 4)); re

我正试图为遥控飞机建立一个简单的地面控制站。我几乎完成了,但是我在校验和计算方面遇到了很多麻烦。我知道Java和C#的数据类型是不同的。我试图解释这一点,但我不确定我是否成功。该程序采用CRC-16-CCITT方法

这是我的端口:

public int crc_accumulate(int b, int crc) {
        int ch = (b ^ (crc & 0x00ff));
        ch = (ch ^ (ch << 4));
        return ((crc >> 8) ^ (ch << 8) ^ (ch << 3) ^ (ch >> 4));
}

public byte[] crc_calculate() {
        int[] pBuffer=new int[]{255,9,19,1,1,0,0,0,0,0,2,3,81,4,3};
        int crcEx=0;
        int clength=pBuffer.length;
        int[] X25_INIT_CRC=new int[]{255,255};
        byte[] crcTmp=new byte[]{(byte)255,(byte)255};
        int crcTmp2 = ((crcTmp[0] & 0xff) << 8) | (crcTmp[1] & 0xff);
        crcTmp[0]=(byte)crcTmp2;
        crcTmp[1]=(byte)(crcTmp2 >> 8);
        System.out.println("pre-calculation: 0x"+Integer.toHexString((crcTmp[0]&0xff))+" 0x"+Integer.toHexString((crcTmp[1]&0xff))+";   ushort: "+crcTmp2);
        if (clength < 1) {
                System.out.println("clength < 1");
                return crcTmp;
        }
        for (int i=1; i<clength; i++) {
                crcTmp2 = crc_accumulate(pBuffer[i], crcTmp2);
        }
        crcTmp[0]=(byte)crcTmp2;
        crcTmp[1]=(byte)(crcTmp2 >> 8);
        System.out.print("crc calculation: 0x"+Integer.toHexString((crcTmp[0]&0xff))+" 0x"+Integer.toHexString((crcTmp[1]&0xff))+";   ushort: "+crcTmp2);
        if (crcEx!=-1) {
                System.out.println("  extraCRC["+crcEx+"]="+extraCRC[crcEx]);
                crcTmp2=crc_accumulate(extraCRC[crcEx], crcTmp2);
                crcTmp[0]=(byte)crcTmp2;
                crcTmp[1]=(byte)(crcTmp2 >> 8);
        System.out.println("with extra CRC:  0x"+Integer.toHexString((crcTmp[0]&0xff))+" 0x"+Integer.toHexString((crcTmp[1]&0xff))+";   ushort: "+crcTmp2+"\n\n");
        }
        return crcTmp;
}
public int-crc\u累计(int-b,int-crc){
int ch=(b^(crc&0x00ff));
ch=(ch^(ch>8)^(ch 4));
}
公共字节[]crc_计算(){
int[]pBuffer=newint[]{255,9,19,1,1,0,0,0,0,2,3,81,4,3};
int-crcEx=0;
int clength=pBuffer.length;
int[]X25_INIT_CRC=新的int[]{255255};
字节[]crcTmp=新字节[]{(字节)255,(字节)255};
int crcTmp2=((crcTmp[0]&0xff)>8);
System.out.println(“预计算:0x”+整数.toHexString((crcTmp[0]&0xff))+“0x”+整数.toHexString((crcTmp[1]&0xff))+“ushort:”+crcTmp2);
如果(长度<1){
系统输出打印长度(“长度<1”);
返回crcTmp;
}
对于(int i=1;i>8);
系统输出打印(“crc计算:0x”+整数.toHexString((crcTmp[0]&0xff))+“0x”+整数.toHexString((crcTmp[1]&0xff))+“ushort:”+crcTmp2);
如果(crcEx!=-1){
System.out.println(“extraCRC[“+crcEx+”]=“+extraCRC[crcEx]);
crcTmp2=crc_累加(extraCRC[crcEx],crcTmp2);
crcTmp[0]=(字节)crcTmp2;
crcTmp[1]=(字节)(crcTmp2>>8);
System.out.println(“带有额外的CRC:0x”+整数.toHexString((crcTmp[0]&0xff))+“0x”+整数.toHexString((crcTmp[1]&0xff))+“ushort:+crcTmp2+”\n\n”);
}
返回crcTmp;
}
这是原始C#文件:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间ArdupilotMega
{
类MavlinkCRC
{
常量int X25_INIT_CRC=0xffff;
const int X25_VALIDATE_CRC=0xf0b8;
公共静态ushort crc_累加(字节b,ushort crc)
{
未经检查
{
字节ch=(字节)(b^(字节)(crc&0x00ff));
ch=(字节)(ch^(ch>8)^(ch 4));
}
}
公共静态ushort crc_计算(字节[]pBuffer,整数长度)
{
如果(长度<1)
{
返回0xffff;
}
//对于包含在无符号字符数组中的长度为字节的“消息”
//由pBuffer指向,计算CRC
//CRC计算(无符号字符*pBuffer,整数长度,无符号短*校验常量)<不需要
乌肖特crcTmp;
int i;
crcTmp=X25初始CRC;
对于(i=1;i
我很确定我的端口的问题在1号线和5号线之间。我希望得到一个0x94 0x88的输出,但是程序输出0x2D 0xF4

如果有人能告诉我哪里出了问题,我将不胜感激

谢谢你的帮助,
Cameron

好吧,首先让我们稍微清理一下C代码:

const int X25_INIT_CRC = 0xffff;

public static ushort crc_accumulate(byte b, ushort crc)
{
    unchecked
    {
        byte ch = (byte)(b ^ (byte)(crc & 0x00ff));
        ch = (byte)(ch ^ (ch << 4));

        return (ushort)((crc >> 8) ^ (ch << 8) ^ (ch << 3) ^ (ch >> 4));
    }
}

public static ushort crc_calculate(byte[] pBuffer)
{
    ushort crcTmp = X25_INIT_CRC;

    for (int i = 1; i < pBuffer.Length; i++) // skips header U
        crcTmp = crc_accumulate(pBuffer[i], crcTmp);

    return crcTmp;
}

对于您问题中的输入(
{255,9,19,1,1,0,0,0,0,0,2,3,81,4,3}
),原始C#,清理过的C#和Java都会生成
0xfc7e

,请将原始代码的相关部分以及您对端口的尝试发布在您问题的正文中,而不是作为链接,您使用不同的类型进行crc计算。为什么不尝试使用相同(或类似)的数据类型,并进行适当的强制转换?@Cameron:请在这里发布您感兴趣的代码。很多人因为担心污染而不去链接,或者因为防火墙而不能去链接。你的问题几乎肯定与符号扩展有关。C#中的字节是无符号的,Java中的字节是有符号的。因此,当分配给int时,值为255的字节变为-1。@karoberts:是的,这是非常可能的。当我将C#程序转换为Java时,我将C#的“byte”更改为Java的“int”。哇,没想到会有如此快速和完整的响应!非常感谢你。我现在得去上班了,但我一回来就会测试一下。再次感谢!我已经将您的代码应用于我的程序,并且我也得到了0xFC 0x7E作为输出。因为有人提供了LabView计算器,所以我希望使用0x94 0x88,但由于清理后的C#和代码的C#几乎相同,我确信这是LabView计算器中的一个错误。谢谢@Cameronlatz正如我在回答的最后所说,链接中的原始C代码也会导致
0xfc7e
。可能是对输入数组进行了双重检查,可能是您遗漏了一个条目或转置了一个数字,等等。事实上,问题是我忘了添加“crc额外值”(我在最初的Java程序中包含了它,但忘了提及)。现在我已经完成了,输出是0x94 0x88。这个问题困扰了我好几个星期了——verdesmarald在几分钟内就解决了,真是疯了!再次感谢你,你帮了我大忙。
const int X25_INIT_CRC = 0xffff;

public static ushort crc_accumulate(byte b, ushort crc)
{
    unchecked
    {
        byte ch = (byte)(b ^ (byte)(crc & 0x00ff));
        ch = (byte)(ch ^ (ch << 4));

        return (ushort)((crc >> 8) ^ (ch << 8) ^ (ch << 3) ^ (ch >> 4));
    }
}

public static ushort crc_calculate(byte[] pBuffer)
{
    ushort crcTmp = X25_INIT_CRC;

    for (int i = 1; i < pBuffer.Length; i++) // skips header U
        crcTmp = crc_accumulate(pBuffer[i], crcTmp);

    return crcTmp;
}
public static final int X25_INIT_CRC = 0xffff;

public static int crc_accumulate(short b, int crc) {
    short ch = (short)((b ^ crc) & 0xff);
    ch = (short)((ch ^ (ch << 4)) & 0xff);

    return ((crc >> 8) ^ (ch << 8) ^ (ch << 3) ^ (ch >> 4)) & 0xffff;
}

public static int crc_calculate(short[] pBuffer) {
    int crcTmp = X25_INIT_CRC;

    for (int i = 1; i < pBuffer.length; i++) // skips header U
        crcTmp = crc_accumulate(pBuffer[i], crcTmp);

    return crcTmp;
}