将C#代码转换为Java

将C#代码转换为Java,c#,java,C#,Java,这些C#代码用于CRC(CycleCredundancyCheck),运行正常 public static void ByteCRC(ref int CRC, char Ch) { int genPoly = 0x18005; CRC ^= (Ch << 8); for (int i = 0; i < 8; i++) if ((CRC & 0x8000) != 0)

这些C#代码用于CRC(CycleCredundancyCheck),运行正常

    public static void ByteCRC(ref int CRC, char Ch)
    {
        int genPoly = 0x18005;
        CRC ^= (Ch << 8);
        for (int i = 0; i < 8; i++)
            if ((CRC & 0x8000) != 0)
                CRC = (CRC << 1) ^ genPoly;
            else
                CRC <<= 1;
        CRC &= 0xffff;
    }

    public static int BlockCRC(String Block)
    {
        int BlockLen = Block.Length;
        int CRC = 0;
        for (int i = 0; i < BlockLen; i++)
            ByteCRC(ref CRC, Block[i]);
        return CRC;
    }

    //Invoking the function
    String data="test"; //testing string
    Console.WriteLine(BlockCRC(data).ToString("X4"));
公共静态无效字节CRC(ref int CRC,char Ch)
{
int-genPoly=0x18005;
CRC^=(Ch)
Java是否具有与C#中的“ToString('X4')”相同的功能


Java有
Format
类,该类的后代
NumberFormat
DecimalFormat
DateFormat
等。请尝试此演示版本,看看与您正在寻找的函数等效的是什么:您有一个全局
CRC
,它根本没有被使用,因为您的参数仍然被称为de>CRC
ByteCRC
函数中对
CRC
的每个引用都引用了局部变量,因此您的更改不会进入全局
CRC
-它保持为零。谢谢!您是对的!ByteCRC是
void
。为什么不返回结果而不是使用全局变量?当然,这会改变签名。谢谢!我找到了函数:Integer.toHexString((int)chars[I])。
    public static int CRC;
    public static void ByteCRC(int CRC, char Ch)
    {
        int genPoly = 0x18005;
        CRC ^= (Ch << 8);
        for (int i = 0; i < 8; i++)
            if ((CRC & 0x8000) != 0)
                CRC = (CRC << 1) ^ genPoly;
            else
                CRC <<= 1;
        CRC &= 0xffff;
    }

    public static int BlockCRC(String Block)
    {
        int BlockLen = Block.length();
        CRC = 0;
        for (int i = 0; i < BlockLen; i++)
            ByteCRC(CRC, Block.charAt(i));
        return CRC;
    }

    //Invoking the function
    String data="test"; //testing string
    System.out.println(BlockCRC(data));