長いキャストがキャストされていることはわかっていますが、これを行うより良い方法があります。32ビット整数が範囲外の量だけ移動した
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
BTW: '&0xFF'の代わりに'&0xFFL'を使うと、 'long'値が得られます。 –