go整型和字节数组之间的转换

主机字节序

主机字节序模式有两种,大端数据模式和小端数据模式,在网络编程中应注意这两者的区别,以保证数据处理的正确性;例如网络的数据是以大端数据模式进行交互,而我们的主机大多数以小端模式处理,如果不转换,数据会混乱 参考 ;一般来说,两个主机在网络通信需要经过如下转换过程:主机字节序 —> 网络字节序 -> 主机字节序

大端小端区别

大端模式:Big-Endian就是高位字节排放在内存的低地址端,低位字节排放在内存的高地址端

低地址 --------------------> 高地址

高位字节                     地位字节

小端模式:Little-Endian就是低位字节排放在内存的低地址端,高位字节排放在内存的高地址端

低地址 --------------------> 高地址

低位字节                     高位字节

高位字节和低位字节概念

例如在32位系统中,257转换成二级制为:00000000 00000000 00000001 00000001,其中

00000001 | 00000001

高位字节     低位字节

int和byte转换

在go语言中,byte其实是uint8的别名,byte 和 uint8 之间可以直接进行互转。目前只能将0~255范围的int转成byte。因为超出这个范围,go在转换的时候,就会把多出来数据扔掉;如果需要将int32转成byte类型,我们需要一个长度为4的[]byte数组就

大端模式下

func f2() {
    var v2 uint32
    var b2 [4]byte
    v2 = 257
    // 将 257转成二进制就是
    // | 00000000 | 00000000 | 00000001 | 00000001 |
    // 	| b2[0]    | b2[1]    | b2[2]    | b2[3]    | // 这里表示b2数组每个下标里面存放的值
    // 这里直接使用将uint32强转成uint8
    // | 00000000 0000000 00000001 | 00000001  直接转成uint8后等于 1
    // |---这部分go在强转的时候扔掉---|
    b2[3] = uint8(v2)
    // | 00000000 | 00000000 | 00000001 | 00000001 | 右移8位 转成uint8后等于 1
    // 下面是右移后的数据
    // |          | 00000000 | 00000000 | 00000001 |
    b2[2] = uint8(v2 >> 8)
    // | 00000000 | 00000000 | 00000001 | 00000001 | 右移16位 转成uint8后等于 0
    // 下面是右移后的数据
    // |          |          | 00000000 | 00000000 |
    b2[1] = uint8(v2 >> 16)
    // | 00000000 | 00000000 | 00000001 | 00000001 | 右移24位 转成uint8后等于 0
    // 下面是右移后的数据
    // |          |          |          | 00000000 |
    b2[0] = uint8(v2 >> 24)
    fmt.Printf("%+v\n", b2)
    // 所以最终将uint32转成[]byte数组输出为
    // [0 0 1 1]
}

小端模式下

小端刚好和大端相反,所以在转成小端模式的时候,只要将[]byte数组的下标首尾对换一下位置就可以了

func f3() {
  var v3 uint32
  var b3 [4]byte
  v3 = 257
  // 将 257转成二进制就是
  // | 00000000 | 00000000 | 00000001 | 00000001 |
  // | b3[0]  | b3[1]  | b3[2]  | b3[3]   | // 这里表示b3数组每个下标里面存放的值
  // 这里直接使用将uint32l强转成uint8
  // | 00000000 0000000 00000001 | 00000001 直接转成uint8后等于 1
  // |---这部分go在强转的时候扔掉---|
  b3[0] = uint8(v3)
  // | 00000000 | 00000000 | 00000001 | 00000001 | 右移8位 转成uint8后等于 1
  // 下面是右移后的数据
  // |     | 00000000 | 00000000 | 00000001 |
  b3[1] = uint8(v3 >> 8)
  // | 00000000 | 00000000 | 00000001 | 00000001 | 右移16位 转成uint8后等于 0
  // 下面是右移后的数据
  // |     |     | 00000000 | 00000000 |
  b3[2] = uint8(v3 >> 16)
  // | 00000000 | 00000000 | 00000001 | 00000001 | 右移24位 转成uint8后等于 0
  // 下面是右移后的数据
  // |     |     |     | 00000000 |
  b3[3] = uint8(v3 >> 24)
  fmt.Printf("%+v\n", b3)
  // 所以最终将uint32转成[]byte数组输出为
  // [1 1 0 0 ]
}

有了上面的的铺垫,go整型和字节数组之间的转换我们也就知道具体操作了,下面上代码

// int 转大端 []byte
func IntToBytesBigEndian(n int64, bytesLength byte) ([]byte, error) {
   switch bytesLength {
   case 1:
      tmp := int8(n)
      bytesBuffer := bytes.NewBuffer([]byte{})
      binary.Write(bytesBuffer, binary.BigEndian, &tmp)
      return bytesBuffer.Bytes(), nil
   case 2:
      tmp := int16(n)
      bytesBuffer := bytes.NewBuffer([]byte{})
      binary.Write(bytesBuffer, binary.BigEndian, &tmp)
      return bytesBuffer.Bytes(), nil
   case 3:
      tmp := int32(n)
      bytesBuffer := bytes.NewBuffer([]byte{})
      binary.Write(bytesBuffer, binary.BigEndian, &tmp)
      return bytesBuffer.Bytes()[1:], nil
   case 4:
      tmp := int32(n)
      bytesBuffer := bytes.NewBuffer([]byte{})
      binary.Write(bytesBuffer, binary.BigEndian, &tmp)
      return bytesBuffer.Bytes(), nil
   case 5:
      tmp := n
      bytesBuffer := bytes.NewBuffer([]byte{})
      binary.Write(bytesBuffer, binary.BigEndian, &tmp)
      return bytesBuffer.Bytes()[3:], nil
   case 6:
      tmp := n
      bytesBuffer := bytes.NewBuffer([]byte{})
      binary.Write(bytesBuffer, binary.BigEndian, &tmp)
      return bytesBuffer.Bytes()[2:], nil
   case 7:
      tmp := n
      bytesBuffer := bytes.NewBuffer([]byte{})
      binary.Write(bytesBuffer, binary.BigEndian, &tmp)
      return bytesBuffer.Bytes()[1:], nil
   case 8:
      tmp := n
      bytesBuffer := bytes.NewBuffer([]byte{})
      binary.Write(bytesBuffer, binary.BigEndian, &tmp)
      return bytesBuffer.Bytes(), nil
   }
   return nil, fmt.Errorf("IntToBytesBigEndian b param is invaild")
}

//int 转小端 []byte
func IntToBytesLittleEndian(n int64, bytesLength byte) ([]byte, error) {
   switch bytesLength {
   case 1:
      tmp := int8(n)
      bytesBuffer := bytes.NewBuffer([]byte{})
      binary.Write(bytesBuffer, binary.LittleEndian, &tmp)
      return bytesBuffer.Bytes(), nil
   case 2:
      tmp := int16(n)
      bytesBuffer := bytes.NewBuffer([]byte{})
      binary.Write(bytesBuffer, binary.LittleEndian, &tmp)
      return bytesBuffer.Bytes(), nil
   case 3:
      tmp := int32(n)
      bytesBuffer := bytes.NewBuffer([]byte{})
      binary.Write(bytesBuffer, binary.LittleEndian, &tmp)
      return bytesBuffer.Bytes()[0:3], nil
   case 4:
      tmp := int32(n)
      bytesBuffer := bytes.NewBuffer([]byte{})
      binary.Write(bytesBuffer, binary.LittleEndian, &tmp)
      return bytesBuffer.Bytes(), nil
   case 5:
      tmp := n
      bytesBuffer := bytes.NewBuffer([]byte{})
      binary.Write(bytesBuffer, binary.LittleEndian, &tmp)
      return bytesBuffer.Bytes()[0:5], nil
   case 6:
      tmp := n
      bytesBuffer := bytes.NewBuffer([]byte{})
      binary.Write(bytesBuffer, binary.LittleEndian, &tmp)
      return bytesBuffer.Bytes()[0:6], nil
   case 7:
      tmp := n
      bytesBuffer := bytes.NewBuffer([]byte{})
      binary.Write(bytesBuffer, binary.LittleEndian, &tmp)
      return bytesBuffer.Bytes()[0:7], nil
   case 8:
      tmp := n
      bytesBuffer := bytes.NewBuffer([]byte{})
      binary.Write(bytesBuffer, binary.LittleEndian, &tmp)
      return bytesBuffer.Bytes(), nil
   }
   return nil, fmt.Errorf("IntToBytesLittleEndian b param is invaild")
}

//(大端) []byte 转 uint
func BytesToUIntBigEndian(b []byte) (int, error) {
   if len(b) == 3 {
      b = append([]byte{0}, b...)
   }
   bytesBuffer := bytes.NewBuffer(b)
   switch len(b) {
   case 1:
      var tmp uint8
      err := binary.Read(bytesBuffer, binary.BigEndian, &tmp)
      return int(tmp), err
   case 2:
      var tmp uint16
      err := binary.Read(bytesBuffer, binary.BigEndian, &tmp)
      return int(tmp), err
   case 4:
      var tmp uint32
      err := binary.Read(bytesBuffer, binary.BigEndian, &tmp)
      return int(tmp), err
   default:
      return 0, fmt.Errorf("%s", "BytesToInt bytes lenth is invaild!")
   }
}

//(大端) []byte 转 int
func BytesToIntBigEndian(b []byte) (int, error) {
   if len(b) == 3 {
      b = append([]byte{0}, b...)
   }
   bytesBuffer := bytes.NewBuffer(b)
   switch len(b) {
   case 1:
      var tmp int8
      err := binary.Read(bytesBuffer, binary.BigEndian, &tmp)
      return int(tmp), err
   case 2:
      var tmp int16
      err := binary.Read(bytesBuffer, binary.BigEndian, &tmp)
      return int(tmp), err
   case 4:
      var tmp int32
      err := binary.Read(bytesBuffer, binary.BigEndian, &tmp)
      return int(tmp), err
   default:
      return 0, fmt.Errorf("%s", "BytesToInt bytes lenth is invaild!")
   }
}

//(小端) []byte 转 uint
func BytesToUIntLittleEndian(b []byte) (int, error) {
   if len(b) == 3 {
      b = append([]byte{0}, b...)
   }
   bytesBuffer := bytes.NewBuffer(b)
   switch len(b) {
   case 1:
      var tmp uint8
      err := binary.Read(bytesBuffer, binary.LittleEndian, &tmp)
      return int(tmp), err
   case 2:
      var tmp uint16
      err := binary.Read(bytesBuffer, binary.LittleEndian, &tmp)
      return int(tmp), err
   case 4:
      var tmp uint32
      err := binary.Read(bytesBuffer, binary.LittleEndian, &tmp)
      return int(tmp), err
   default:
      return 0, fmt.Errorf("%s", "BytesToInt bytes lenth is invaild!")
   }
}

// 小端[]byte 转 int
func BytesToIntLittleEndian(b []byte) (int, error) {
   if len(b) == 3 {
      b = append([]byte{0}, b...)
   }
   bytesBuffer := bytes.NewBuffer(b)
   switch len(b) {
   case 1:
      var tmp int8
      err := binary.Read(bytesBuffer, binary.LittleEndian, &tmp)
      return int(tmp), err
   case 2:
      var tmp int16
      err := binary.Read(bytesBuffer, binary.LittleEndian, &tmp)
      return int(tmp), err
   case 4:
      var tmp int32
      err := binary.Read(bytesBuffer, binary.LittleEndian, &tmp)
      return int(tmp), err
   default:
      return 0, fmt.Errorf("%s", "BytesToInt bytes lenth is invaild!")
   }
}

参考:

https://blog.csdn.net/skh2015java/article/details/80751229

https://www.jb51.net/article/150599.htm

 

你可能感兴趣的:(Golang)