我知道有一个缺少强制转换为长转移,但有一个更好的方式来做到这一点比所示计算策略减少混乱。32位的int由量不在范围
static long getLong(byte[] sourceBytes, int sourceBytesIndex, int numOfBytesToConvert)
{
long longValue = 0;
longValue = (sourceBytes[sourceBytesIndex] & 0xFF) +
((sourceBytes[sourceBytesIndex + 1] & 0xFF) << 8);
if (numOfBytesToConvert > 2)
{
longValue += ((sourceBytes[sourceBytesIndex + 2] & 0xFF) << 16) +
((sourceBytes[sourceBytesIndex + 3] & 0xFF) << 24);
if (numOfBytesToConvert > 4)
{
longValue += ((sourceBytes[sourceBytesIndex + 4] & 0xFF) << 32) +
((sourceBytes[sourceBytesIndex + 5] & 0xFF) << 40);
if (numOfBytesToConvert > 6)
{
longValue += ((sourceBytes[sourceBytesIndex + 6] & 0xFF) << 48) +
((sourceBytes[sourceBytesIndex + 7] & 0xFF) << 56);
}
}
}
return longValue;
}
怎么样一个简单的循环? – Voo 2012-03-15 10:59:07
顺便说一句:使用'&0xFFL'而不是'&0xFF'会给你'长'值。 – 2012-03-15 11:19:44