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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章