using System; using System.Collections.Generic; using System.Linq; using System.Text; public class Test { class _BitConverter { private Byte[] tmpfor_1ByteToInt32 = new Byte[4]; private Byte[] tmpfor_2ByteToInt32 = new Byte[4]; private Byte[] tmpfor_3ByteToInt32 = new Byte[4]; private static Int32 generalToInt32(Byte[] bytes, int bitsCount) { Int32 shiftCount = 32 - bitsCount; UInt32 ui = BitConverter.ToUInt32(bytes, 0); UInt32 ui2 = ui << shiftCount; Int32 i = (Int32)(ui2); Int32 i2 = i >> shiftCount; return i2; } public Int32 C1ByteToInt32(Byte[] bytes, int startIndex) { tmpfor_1ByteToInt32[0] = bytes[0 + startIndex]; return generalToInt32(tmpfor_1ByteToInt32, 8); } public Int32 C2ByteToInt32(Byte[] bytes, int startIndex) { tmpfor_2ByteToInt32[0] = bytes[0 + startIndex]; tmpfor_2ByteToInt32[1] = bytes[1 + startIndex]; return generalToInt32(tmpfor_2ByteToInt32, 16); } public Int32 C3ByteToInt32(Byte[] bytes, int startIndex) { tmpfor_3ByteToInt32[0] = bytes[0 + startIndex]; tmpfor_3ByteToInt32[1] = bytes[1 + startIndex]; tmpfor_3ByteToInt32[2] = bytes[2 + startIndex]; return generalToInt32(tmpfor_3ByteToInt32, 24); } } public static void Main(string[] args) { var converter = new _BitConverter(); Console.WriteLine(converter.C1ByteToInt32(new byte[] { 0xCC, 0xCC, 0xFE, 0xFF, 0x80 }, 3)); Console.WriteLine(converter.C2ByteToInt32(new byte[] { 0xCC, 0xCC, 0xFE, 0xFF, 0x80 }, 2)); Console.WriteLine(converter.C3ByteToInt32(new byte[] { 0xCC, 0xCC, 0xFE, 0xFF, 0x80 }, 2)); } }