• C#int类型 转Byte[]


    方法1:使用左移和右移

    int转化为byte[]:
      public  byte[] intToBytes(int value)
            {
                byte[] src = new byte[4];
                src[3] = (byte)((value >> 24) & 0xFF);
                src[2] = (byte)((value >> 16) & 0xFF);
                src[1] = (byte)((value >> 8) & 0xFF);//高8位
                src[0] = (byte)(value & 0xFF);//低位
                return src;
            }
    byte[]转化为int:
    public  int bytesToInt(byte[] src, int offset)
            {
                int value;
                value = (int)((src[offset] & 0xFF)
                        | ((src[offset + 1] & 0xFF) << 8)
                        | ((src[offset + 2] & 0xFF) << 16)
                        | ((src[offset + 3] & 0xFF) << 24));
                return value;
            }  
    方法2:使用BitConverter
    int转化为byte[]:
        public  byte[] IntToBitConverter(int num)
            {
                byte[] bytes = BitConverter.GetBytes(num);
                return bytes;
            }
    byte[]转化为int:
      public  int IntToBitConverter(byte[] bytes)
            {
              int  temp = BitConverter.ToInt32(bytes, 0);
              return temp;
            }
  • 相关阅读:
    Java IO/NIO
    LeetCode—— 两两交换链表中的节点
    LeetCode——合并K个排序链表
    LeetCode第201场周赛
    LeetCode第202场周赛
    LeetCode215. 数组中的第K个最大元素
    LeetCode213. 打家劫舍 II
    LeetCode212. 单词搜索 II
    LeetCode211. 添加与搜索单词
    LeetCode210. 课程表 II
  • 原文地址:https://www.cnblogs.com/LCLBook/p/11547158.html
Copyright © 2020-2023  润新知