开发者

blackberry CRC32 issue

I've been stuck on this for a few days now and can't seem to get it. I'm hoping that by posting here, I'll have an epiphany and fix this issue 30 seconds after posting! :D Here goes....

I need to encrypt a string, append it to my URL, and send it to the server. The steps are as follows:

1. Build the string (for this post, I'm using a constant TEST string)
2. Get a CRC of the string and add that to the front of the string.
3. HEX the whole string.
4. Append to URL and connect over HTTP

I'm stuck on 2!! The CRC that I SHOULD be getting is 1903129755, but among other results, I'm getting -1903129756. NOTE: For purposes of this post, I'm using a test string and its resulting CRC. The string will change as I include the time in milliseconds when building it.

I should point out also, this is on blackberry. The android code works perfect. That CRC is taken from both the Android and an online CRC generator website: http://hash.online-convert.com/crc32b-generator

The code I have seems to work for others, so I'm obviously at fault here. Can someone spot my error?? Thanks

As you can see, I've tried a few different ways:

    import net.rim.device.api.util.CRC32;

    public long getCrcValue(String inputText)
    {
            int crc1 = 0,crc2=0;
            long crc3=0;

            crc1 = CRC32.update(CRC32.INITIAL_VALUE, inputText.getBytes());
            crc2 = CRC32.update(0, inputText.getBytes());
            String temp = Integer.toBinaryString(crc1);
            crc3 = Long.parseLong(temp,2);
            long crc4 = CRC32.update(CRC32.INITIAL_VALUE, inputText.getBytes());
            long crc5 = CRC32.update(0, inputText.getBytes());


            logger.log("CRC1 is: "+crc1);
            logger.log("CRC2 is: "+crc2);
            logger.log("CRC3 is: "+crc3);
            logger.log("CRC4 is: "+crc4);
            logger.log("CRC5 is: "+crc5);

return crc1;
}

PS logger is my own class to output customised formatted outputs to the console, so ignore it.

EDIT: Speaking of outputs, here's what the output is:

CRC1 is: -1903129756
CRC2 is: -46083367开发者_StackOverflow中文版6
CRC3 is: 2391837540
CRC4 is: -1903129756
CRC5 is: -460833676

EDIT2: Here's the equivalent Android code. Note crc is defined as CRC32 crc:

public long getCrcValue(String inputText)
{
    crc.reset();
    crc.update(inputText.getBytes());
    return crc.getValue();
}


FYI, Eventually I fixed this by taking the source from java.util.zip.CRC32 and java.util.zip.checksum and creating new classes in my blackberry project using that source. Since thats the same implementation that Android uses, both apps work fine!


Here is how it should be:

int crc32 = CRC32.update(CRC32.INITIAL_VALUE, inputText.getBytes()) ^ 0xffffffff;


String temp = Integer.toBinaryString(crc1);
crc3 = Long.parseLong(temp,2);

Off topic but what on earth is that? Are you trying to do 'long crc3 = crc1;' the hardest possible way?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜