fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. public class Test
  7. {
  8. class _BitConverter
  9. {
  10. private Byte[] tmpfor_1ByteToInt32 = new Byte[4];
  11. private Byte[] tmpfor_2ByteToInt32 = new Byte[4];
  12. private Byte[] tmpfor_3ByteToInt32 = new Byte[4];
  13.  
  14. private static Int32 generalToInt32(Byte[] bytes, int bitsCount)
  15. {
  16. Int32 shiftCount = 32 - bitsCount;
  17.  
  18. UInt32 ui = BitConverter.ToUInt32(bytes, 0);
  19. UInt32 ui2 = ui << shiftCount;
  20. Int32 i = (Int32)(ui2);
  21. Int32 i2 = i >> shiftCount;
  22. return i2;
  23. }
  24.  
  25. public Int32 C1ByteToInt32(Byte[] bytes, int startIndex)
  26. {
  27. tmpfor_1ByteToInt32[0] = bytes[0 + startIndex];
  28. return generalToInt32(tmpfor_1ByteToInt32, 8);
  29. }
  30.  
  31. public Int32 C2ByteToInt32(Byte[] bytes, int startIndex)
  32. {
  33. tmpfor_2ByteToInt32[0] = bytes[0 + startIndex];
  34. tmpfor_2ByteToInt32[1] = bytes[1 + startIndex];
  35. return generalToInt32(tmpfor_2ByteToInt32, 16);
  36. }
  37.  
  38. public Int32 C3ByteToInt32(Byte[] bytes, int startIndex)
  39. {
  40. tmpfor_3ByteToInt32[0] = bytes[0 + startIndex];
  41. tmpfor_3ByteToInt32[1] = bytes[1 + startIndex];
  42. tmpfor_3ByteToInt32[2] = bytes[2 + startIndex];
  43. return generalToInt32(tmpfor_3ByteToInt32, 24);
  44. }
  45. }
  46.  
  47. public static void Main(string[] args)
  48. {
  49. var converter = new _BitConverter();
  50. Console.WriteLine(converter.C1ByteToInt32(new byte[] { 0xCC, 0xCC, 0xFE, 0xFF, 0x80 }, 3));
  51. Console.WriteLine(converter.C2ByteToInt32(new byte[] { 0xCC, 0xCC, 0xFE, 0xFF, 0x80 }, 2));
  52. Console.WriteLine(converter.C3ByteToInt32(new byte[] { 0xCC, 0xCC, 0xFE, 0xFF, 0x80 }, 2));
  53. }
  54. }
Success #stdin #stdout 0.03s 37008KB
stdin
Standard input is empty
stdout
-1
-2
-8323074