我知道有一個缺少強制轉換爲長轉移,但有一個更好的方式來做到這一點比所示計算策略減少混亂。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