C# 整数和byte数组互换

   1:          public static byte[] int2Byte(int intValue)
   2:          {
   3:              byte[] b = new byte[4];
   4:              for (int i = 0; i < 4; i++)
   5:              {
   6:                  b[i] = (byte)(intValue >> 8 * (i) & 0xFF);
   7:              }
   8:              return b;
   9:          }
  10:   
  11:          // 将iSource转为长度为iArrayLen的byte数组,字节数组的低位是整型的低字节位
  12:          public static byte[] int2Byte(int iSource, int len)
  13:          {
  14:              byte[] b = new byte[len];
  15:              for (int i = 0; (i < 4) && (i < len); i++)
  16:              {
  17:                  b[i] = (byte)(iSource >> 8 * i & 0xFF);
  18:              }
  19:              return b;
  20:          }
  21:   
  22:          // 将byte数组bRefArr转为一个整数,字节数组的低位是整型的低字节位
  23:          public static int byte2Int(byte[] b)
  24:          {
  25:              int iOutcome = 0;
  26:              byte bLoop;
  27:              for (int i = 0; i < 4; i++)
  28:              {
  29:                  bLoop = b[i];
  30:                  iOutcome += (bLoop & 0xFF) << (8 * i);
  31:              }
  32:              return iOutcome;
  33:          }
posted @ 2012-03-06 16:54  梅子黄时雨  阅读(1840)  评论(0编辑  收藏  举报