int and byte conversions in go

  • 2020-06-23 00:38:00
  • OfStack

Host byte order

There are two kinds of host byte order mode, big-end data mode and small-end data mode. In network programming, we should pay attention to the difference between the two to ensure the correctness of data processing. For example, network data is interacted in large-terminal data mode, while most of our hosts are processed in small-terminal mode. If the data is not converted, the reference will be confused. In general, two hosts communicate over the network through the following conversion process: host byte order -- > Network byte order - > Host byte order

Difference between big end and small end

Big-end mode: ES9en-ES10en is the high-byte emission at the low-address end of memory, and the low-byte emission at the high-address end of memory
Lower address -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > High address
High byte position byte
Small end mode: Little-ES16en is the low address end of low byte emission in memory, and the high address end of high byte emission in memory
Lower address -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > High address
Low byte high byte

What are the high byte and low byte

For example, in the 32-bit system, 357 is converted to the 2-level system as: 000000000000000000001 01100101, where

00000001 | 01100101
High byte low byte

int and byte conversions

In the go language, byte is actually an alias for uint8, with direct interchanges between byte and uint8. Currently, only int in the range 0~255 can be converted to byte. Because of this, go throws away the extra data during conversion; If we need to convert int32 to byte, we only need one []byte array of length 4

Big end mode

func f2() {
var v2 uint32
var b2 [4]byte
v2 = 257
// Convert 257 to base 2
// | 00000000 | 00000000 | 00000001 | 00000001 |
// | b2[0] | b2[1] | b2[2] | b2[3] | //
// Use uint32 to convert uint8 directly
// | 000000000000000001| 00000001 is equal to 1 after converting directly to uint8
// |-- this part of go is thrown away on strong turn --|
b2[3] = uint8(v2)
So if you shift 8 to the right and convert to uint8, you get 1
// The following is the data after the right shift
// | | 00000000 | 00000000 | 00000001 |
b2[2] = uint8(v2 > > 8)
So if you shift 16 bits to the right and convert to uint8, you get 0
// The following is the data after the right shift
// | | | 00000000 | 00000000 |
b2[1] = uint8(v2 > > 16)
If you shift 24 bits to the right and convert to uint8, you get 0
// The following is the data after the right shift
// | | | | 00000000 |
b2[0] = uint8(v2 > > 24)
fmt.Printf("%+v\n", b2)
// So uint32 is finally converted into []byte array output is
// [0 0 1 1]
}

In small end mode


//  And we saw above that the small end is just the opposite of the big end, so when you switch to the small end, you just take the []byte The index of the array is reversed 1 The lower position will do 
func f3() {
  var v3 uint32
  var b3 [4]byte
  v3 = 257
  //  will  256 into 2 Hexadecimal is 
  // | 00000000 | 00000000 | 00000001 | 00000001 |
  // | b3[0]  | b3[1]  | b3[2]  | [3]   | //  Here said b3 Array of values stored in each index 
  //  Here we use will directly uint32l Equivalent to uint8
  // | 00000000 0000000 00000001 | 00000001  Directly to uint8 After equal to  1
  // |--- This part of the go Throw it away when you turn it hard ---|
  b3[0] = uint8(v3)
  // | 00000000 | 00000000 | 00000001 | 00000001 |  Moves to the right 8 position   into uint8 After equal to  1
  //  So here's the data that's shifted to the right 
  // |     | 00000000 | 00000000 | 00000001 |
  b3[1] = uint8(v3 >> 8)
  // | 00000000 | 00000000 | 00000001 | 00000001 |  Moves to the right 16 position   into uint8 After equal to  0
  //  So here's the data that's shifted to the right 
  // |     |     | 00000000 | 00000000 |
  b3[2] = uint8(v3 >> 16)
  // | 00000000 | 00000000 | 00000001 | 00000001 |  Moves to the right 24 position   into uint8 After equal to  0
  //  So here's the data that's shifted to the right 
  // |     |     |     | 00000000 |
  b3[3] = uint8(v3 >> 24)
  fmt.Printf("%+v\n", b3)
  //  So eventually uint32 into []byte Array output is 
  // [1 1 0 0 ]
}

Convert go demo


// The plastic is converted to bytes 
func IntToBytes(n int) []byte {
  x := int32(n)
  bytesBuffer := bytes.NewBuffer([]byte{})
  binary.Write(bytesBuffer, binary.BigEndian, x)
  return bytesBuffer.Bytes()
}
// Bytes converted to plastic 
func BytesToInt(b []byte) int {
  bytesBuffer := bytes.NewBuffer(b)

  var x int32
  binary.Read(bytesBuffer, binary.BigEndian, &x)

  return int(x)
}

conclusion


Related articles: