GO language study notes 3-int and byte type conversion

1. Host endian

There are two main endian mode, big-endian and little-endian mode, data mode data. Network Programming should be noted that difference between the two, to ensure correct data processing. For example, the data network is based on big-endian mode data interact, and our hosts most of the processing to little-endian mode, if not converted, the data will be chaos.

Reference: In general, two hosts in the communication network conversion process goes through the following: Host byte order -> network byte order -> host byte order.

2. The difference between the large end of the small end

Big endian: Big-Endian is discharged in the high byte of the memory address terminal of the low order byte address discharge in the high end of memory

Low address --------------------> High Address

High byte Low byte

Little-endian mode: Little-Endian is discharged in the low byte of memory address low end, high byte address discharge in the high end of memory

Low address --------------------> High Address

Low byte High byte

What are the high and low bytes?

For example, in system 32, system 357 is converted into two: 00000000 00000000 0,000,000,101,100,101, wherein

00000001 | 01100101
High byte Low byte

3.int and byte type conversion

In the GO language, uint8 byte is actually an alias, you can direct conversions between byte and uint8. At present, only the range of 0 ~ 255 int converted into byte. Because outside this range, GO at the time of conversion, the extra data will be discarded. If you need to turn into byte type int32, we only need a length [] 4 byte array can be.

Big endian mode
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]
}
Under little-endian mode
// 在上面我们讲过,小端刚好和大端相反的,所以在转成小端模式的时候,只要将[]byte数组的下标首尾对换一下位置就可以了
func f3() {
    var v3 uint32
    var b3 [4]byte
    v3 = 257
    // 将 256转成二进制就是
    // | 00000000 | 00000000 | 00000001 | 00000001 |
    // | b3[0]    | b3[1]   | b3[2]    | [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 ]
}
The sample code
package main

import (
    "bytes"
    "encoding/binary"
    "fmt"
)

//整形转换成字节
func IntToBytes(n int) []byte {
    x := int32(n)

    bytesBuffer := bytes.NewBuffer([]byte{})
    _ = binary.Write(bytesBuffer, binary.BigEndian, x)
    return bytesBuffer.Bytes()
}

//字节转换成整形
func BytesToInt(b []byte) int {
    bytesBuffer := bytes.NewBuffer(b)

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

    return int(x)
}

func main() {
    var a int
    a = 20
    b := []byte {0, 0, 0, 'A'}

    fmt.Println(IntToBytes(a))
    fmt.Println(BytesToInt(b))

}
Code output
[0 0 0 20]
65

Original Post link:

https://studygolang.com/articles/16154


Homepage:

www.codeapes.cn

Guess you like

Origin www.cnblogs.com/codeapes666/p/12093800.html