简体   繁体   English

Java C ++代码转换

[英]Java C++ code conversion

I need to convert this code (in C++) into Java code : 我需要将此代码(在C ++中)转换为Java代码:

    short i;
    short j;
    short k;
    short result;
    unsigned short  m_table[ 256 ]

    for ( i = 0 ; i < 256 ; i++ )
    {
        k = i << 8;
        result = 0;
        for ( j = 0 ; j < 8 ; j++ )
        {
            if ( ( result^ k ) & 0x8000 )   
                result= ( result<< 1 ) ^ 0x1021;
            else
                result<<= 1;
            k <<= 1;
        }
        m_table[ i ] = (unsigned short) result;
    }

...but I never get the same result... ...但是我从来没有得到相同的结果...

My Java code is: 我的Java代码是:

int i;
int j;
int k;
int result;
int m_table[ 256 ] = new int[256];

for ( i = 0 ; i < 256 ; i++ ) {
    k = (i << 8);

    result = 0;

    for ( j = 0 ; j < 8 ; j++ ) {

        if ( (( result^ k ) & 0x8000) != 0)
            result= (( result<< 1 ) ^ 0x1021);
        else
            result<<= 1;

        k <<= 1;
    }

    m_table[ i ] = (result);
}

You need to be careful in Java with bit operations. 在Java中,需要小心使用位操作。 Java does not have any unsigned types, so you probably need to go one type size bigger than what you would use with the unsigned variety. Java没有任何无符号类型,因此您可能需要使用比无符号类型更大的一个类型大小。

As others have stated, upping the size of m_table is a solution. 正如其他人所述,增加m_table的大小是一种解决方案。 However, you will need to be careful in the casting to keep it unsigned. 但是,您在转换时需要小心,以使其保持未签名状态。

Simply doing: 简单地做:

m_table[ i ] = (int)result;

...for example, will carry over the sign bit of result. ...例如,将结转结果的符号位。 So if the signed short result was -1 then m_table[i] will end up with -1 when what you really want is 0xffff. 因此,如果带符号的简短结果为-1,那么当您真正想要的是0xffff时,m_table [i]将以-1结尾。

Fix that with: 使用以下方法解决此问题:

m_table[ i ] = result & 0xffff;

That should give you the equivalent of the original signed short to unsigned short cast... storing it in an int just to maintain the unsigned-ness. 那应该给你相当于原始的有符号的short到无符号的short强制转换...将它存储在一个int中只是为了保持无符号性。

Is the JVM big-endian? JVM是big-endian吗? What happens if you use the constants 0x0080 and 0x2110? 如果使用常量0x0080和0x2110,会发生什么?

Java does integer operations on int s or long s (if present), so you'll need to case the results back to short . Java对intlong (如果存在)进行整数运算,因此您需要将结果大小写为short Also there is no automatic conversion from integers to booleans. 也没有从整数到布尔值的自动转换。 Leaving out braces is a bit taboo. 省略大括号有点禁忌。 So: 所以:

        if ( ( result^ k ) & 0x8000 )   
            result= ( result<< 1 ) ^ 0x1021;
        else
            result<<= 1;

Should become: 应该变成:

        if (((result^k) & 0x8000) == 0) {
            result <<= 1;
        } else {
            result = (short)((result<<1) ^ 0x1021);
        }

The unsigned from unsigned short should go (no almost unsigned in Java, although you could use char ). unsigned from unsigned short应该会去(在Java中几乎没有unsigned,尽管可以使用char )。 Java has the advantage of having reliable ranges and behaviour of integer types. Java的优点是具有可靠的整数类型范围和行为。 Stick to using short if you mean 16-bits. 如果您的意思是16位,请坚持使用short The top 16-bits will be removed, but careful when reading out to & 0xffff . 前16位将被删除,但在读取& 0xffff时要小心。 The original C (or "C++") code is not portable. 原始的C(或“ C ++”)代码不可移植。

The problem you have I believe is with left shifting( << ) int values out of unsigned short range. 我相信您遇到的问题是左移位( <<int值超出unsigned short范围。 To remedy the problem you need to mask the result of left shift into the range. 要解决此问题,您需要掩盖左移到范围内的结果。 The mask to use is 0xFFFF. 使用的掩码是0xFFFF。 The thing to remember is that & is a very low precedence operator, so parenthesis (a lot of them) are in order. 要记住的是&是一个非常低的优先级运算符,因此括号(很多)中的顺序是有序的。 Here is the modified example. 这是修改后的示例。

    final int m_table[ ] = new int[256];

    for ( int i = 0 ; i < 256 ; i++ ) {
        // OK not to mask the result of << here, we are in unsigned short range
        int k = (i << 8); // BTW, terrible name for this variable

        int result = 0;

        for ( int j = 0 ; j < 8 ; j++ ) {

            if ( (( result^ k ) & 0x8000) != 0)
            {
                result= (( (result<< 1) & 0xFFFF ) ^ 0x1021);
            }
            else
            {
                result <<= 1;     // Change to 1-liner if you wish
                result &= 0xFFFF; //
            }

            k <<= 1;     // Change to 1-liner if you wish
            k &= 0xFFFF; //
        }

        m_table[ i ] = result;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM