作为最后的手段,您可以随时移动+添加自己:
byte b1, b2, b3;
int r = b1 << 16 | b2 << 8 | b3;
Run Code Online (Sandbox Code Playgroud)
只需交换b1/b2/b3,直到获得所需的结果.
再想一想,这绝不会产生负面价值.
当msb> = 0x80时,你想要什么结果?
第2部分,暴力标志扩展:
private static int Bytes2Int(byte b1, byte b2, byte b3)
{
int r = 0;
byte b0 = 0xff;
if ((b1 & 0x80) != 0) r |= b0 << 24;
r |= b1 << 16;
r |= b2 << 8;
r |= b3;
return r;
}
Run Code Online (Sandbox Code Playgroud)
我测试过这个:
byte[] bytes = BitConverter.GetBytes(p);
int r = Bytes2Int(bytes[2], bytes[1], bytes[0]);
Console.WriteLine("{0} == {1}", p, r);
Run Code Online (Sandbox Code Playgroud)
好几个p.