yansheng.wang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
        public static byte[] Int32ToBytes(int intValue)
        {
            byte[] bytes = new byte[4];
            bytes[0] = (byte)(intValue & 0x000000FF);
            bytes[1] = (byte)((intValue & 0x0000FF00) >> 8);
            bytes[2] = (byte)((intValue & 0x00FF0000) >> 16);
            bytes[3] = (byte)((intValue & 0xFF000000) >> 24);
            return bytes;
        }

        public static int BytesToInt32(byte[] bytes)
        {
            long longValue = (uint)bytes[0] & 0x000000FF
                | ((uint)bytes[1] << 8) & 0x0000FF00
                | ((uint)bytes[2] << 16) & 0x00FF0000
                | ((uint)bytes[3] << 24) & 0xFF000000;
            return (int)longValue;
        }

        public static byte[] Int16ToBytes(short shortValue)
        {
            byte[] bytes = new byte[2];
            bytes[0] = (byte)(shortValue & 0x000FF);
            bytes[1] = (byte)((shortValue & 0xFF00) >> 8);
            return bytes;
        }


        public static short BytesToInt16(byte[] bytes)
        {
            short longValue = (short)((uint)bytes[0] & 0x00FF
                            | ((uint)bytes[1] << 8) & 0xFF00);
            return longValue;
        }


posted on 2012-06-13 08:34  小小程序员001  阅读(720)  评论(0)    收藏  举报