The Conversion of int and byte in go Language

Time:2019-3-25

Host byte order

There are two host byte-order modes, big-end data mode and small-end data mode. In network programming, we should pay attention to the difference between them to ensure the correctness of data processing. For example, the data of network is interacted in large-end data mode, while most of our hosts are processed in small-end mode, if not converted, the data will be confused for reference; generally speaking, the two hosts are online. Network communication needs to go through the following conversion process: host byte order – > network byte order – > host byte order

Big end and small end differences

Big-Endian mode: Big-Endian is the low address side of memory where high-bit bytes are emitted and the high address side of memory where low-bit bytes are emitted.
Low Address——————————> High Address
High-bit byte
Small-end mode: Little-Endian is the low address side of memory where low-bit bytes are emitted, while high-bit bytes are emitted at the high address side of memory.
Low Address——————————> High Address
Low-byte, High-byte

What are high-bit bytes and low-bit bytes?

For example, in a 32-bit system, 357 is converted into a secondary system: 00000000 00000 00001 100101, where

00000001 | 01100101
High byte and low byte

Int and byte transformations

In go language, byte is actually an alias for uint8, which can be directly intertwined with uint8. At present, only 0-255 range int can be converted to byte. Because beyond this range, go throws out the extra data when converting; if we need to convert int32 to byte, we only need a [] byte array of length 4.

Large-end mode

func f2() {
    var v2 uint32
    var b2 [4]byte
    v2 = 257
// Converting 257 to binary is
    // | 00000000 | 00000000 | 00000001 | 00000001 |
//| B2 [0] | B2 [1] | B2 [2] | B2 [3] | // This represents the value stored in each subscript of the B2 array
// This is where the uint32 power is converted directly to uint8
//| 00000000000000001 | 00000001 is equivalent to 1 when converted directly to uint8
//| This part of go is thrown away when it turns strongly.-|
    b2[3] = uint8(v2)
//| 00000000 | 00000000 | 00000001 | 00000001 | Right shift 8 bits to uint8 equals 1
// Here’s the right-shifted data
    // |          | 00000000 | 00000000 | 00000001 |
    b2[2] = uint8(v2 >> 8)
//| 00000000 | 00000000 | 00000001 | 00000001 | Right shift 16 bits to uint8 equals 0
// Here’s the right-shifted data
    // |          |          | 00000000 | 00000000 |
    b2[1] = uint8(v2 >> 16)
//| 00000000 | 00000000 | 00000001 | 00000001 | Right shift 24 bits to uint 8 equals 0
// Here’s the right-shifted data
    // |          |          |          | 00000000 |
    b2[0] = uint8(v2 >> 24)
    fmt.Printf(“%+v\n”, b2)
// So ultimately the uint32 is converted to the [] byte array output as
    // [0 0 1 1]
}

Small-end mode

// As we said above, the small end is just the opposite of the big end, so when you switch to the small end mode, just change the position of the subscript end of the [] byte array.
func f3() {
  var v3 uint32
  var b3 [4]byte
  v3 = 257
  // Converting 256 to binary is
  // | 00000000 | 00000000 | 00000001 | 00000001 |
  //| B3 [0] | B3 [1] | B3 [2] | [3] | // This represents the value stored in each subscript of the B3 array
  // This is where you can directly convert uint32l strong to uint8
  //| 000 000 000 000 000 000 000 01 | 000 000 01 directly converted to uint 8 equals 1
  //| This part of go is thrown away when it turns strongly.-|
  b3[0] = uint8(v3)
  //| 00000000 | 00000000 | 00000001 | 00000001 | Right shift 8 bits to uint8 equals 1
  // Here's the right-shifted data
  // |     | 00000000 | 00000000 | 00000001 |
  b3[1] = uint8(v3 >> 8)
  //| 00000000 | 00000000 | 00000001 | 00000001 | Right shift 16 bits to uint8 equals 0
  // Here's the right-shifted data
  // |     |     | 00000000 | 00000000 |
  b3[2] = uint8(v3 >> 16)
  //| 00000000 | 00000000 | 00000001 | 00000001 | Right shift 24 bits to uint 8 equals 0
  // Here's the right-shifted data
  // |     |     |     | 00000000 |
  b3[3] = uint8(v3 >> 24)
  fmt.Printf("%+v\n", b3)
  // So ultimately the uint32 is converted to the [] byte array output as
  // [1 1 0 0 ]
}

Go transform demo

// Shaping into bytes
func IntToBytes(n int) []byte {
  x := int32(n)
  bytesBuffer := bytes.NewBuffer([]byte{})
  binary.Write(bytesBuffer, binary.BigEndian, x)
  return bytesBuffer.Bytes()
}
// Byte conversion to shaping
func BytesToInt(b []byte) int {
  bytesBuffer := bytes.NewBuffer(b)

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

  return int(x)
}

summary

Above mentioned is the conversion mode of int and byte in go language introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support to developpaer.

Recommended Today

A detailed explanation of the differences between Perl and strawberry Perl and ActivePerl

Perl is the abbreviation of practical extraction and report language “practical report extraction language”. Application of activestateperl and strawberry PERL on Windows platformcompiler。 Perl   The relationship between the latter two is that C language and Linux system have their own GCC. The biggest difference between activestate Perl and strawberry Perl is that strawberry Perl […]