Golang binary Packet--byte How does an array go int?

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In the C language test, I prefer to test this thing, how to convert a char array into an int type. I've seen it, but I've forgotten it. Later saw this kind binary.BigEndian.Uint32(a) of thing, straight blind. Later went to read the document, looked at half a day also did not understand.

Here directly to say, source code. The following is the code that is the uint8 byte array, size 4, converted to Int32.

package mainimport "fmt"import "encoding/binary"func main() {var a []byte = []byte{0, 1, 2, 3}fmt.Println(a)fmt.Println(binary.BigEndian.Uint32(a))fmt.Println(binary.LittleEndian.Uint32(a))}

Execution Result:

[0 1 2 3]6605150462976

There are two different ways to convert, that is, the big and small ends. The big-endian is the low address in memory corresponding to the high number of integers. According to the above example, it is spelled in 0123 order int32 , the highest 8 bits of the integer is 0, then 1, and so on, so 66051. The small end is the reverse, the highest 8 bits is 3, that is 00000101 , this finally gets 50462976.

In the Golang source directory encoding/binary.go , there is the implementation of the function. However, there is no check on the length of the byte array, if the incoming array length is less than 4, the error will naturally be: panic: runtime error: index out of range .

func (bigEndian) Uint32(b []byte) uint32 {return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24}

The above BigEndian and LittleEndian all are implementation classes, are implemented classes ByteOrder .

type ByteOrder interface {    Uint16([]byte) uint16    Uint32([]byte) uint32    Uint64([]byte) uint64    PutUint16([]byte, uint16)    PutUint32([]byte, uint32)    PutUint64([]byte, uint64)    String() string}

###### References + "1" byte order (Endian), Big End (Big-endian), Small (Little-endian)-led by wife Full Street + "2" Package binary-the Go Programming Language

Original link: Golang binary package--byte How does an array go int? , please specify the source of the reprint!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.