我已经坚持了几天了,似乎无法理解。我希望通过在这里发帖,我会顿悟并在发帖后 30 秒解决这个问题!:D 来了....
我需要加密一个字符串,将其附加到我的 URL,然后将其发送到服务器。步骤如下:
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
我卡在2了!!我应该得到的 CRC 是1903129755
,但除其他结果外,我得到的是-1903129756
。注意:出于本文的目的,我使用了一个测试字符串及其生成的 CRC。当我在构建它时包含以毫秒为单位的时间时,字符串会发生变化。
我还应该指出,这是在黑莓上。android代码完美运行。该 CRC 取自 Android 和在线 CRC 生成器网站:
http://hash.online-convert.com/crc32b-generator
我拥有的代码似乎对其他人有用,所以我显然在这里有错。有人可以发现我的错误吗?谢谢
如您所见,我尝试了几种不同的方法:
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;
}
PSlogger
是我自己的类,用于将自定义格式的输出输出到控制台,所以忽略它。
编辑:说到输出,输出是这样的:
CRC1 is: -1903129756
CRC2 is: -460833676
CRC3 is: 2391837540
CRC4 is: -1903129756
CRC5 is: -460833676
EDIT2:这是等效的 Android 代码。注意crc
定义为CRC32 crc
:
public long getCrcValue(String inputText)
{
crc.reset();
crc.update(inputText.getBytes());
return crc.getValue();
}