byte order of "go" go language

Source: Internet
Author: User

Original: http://lihaoquan.me/2016/11/5/golang-byteorder.html

This person's blog is well written, the quality is also relatively high. I should also have this kind of spirit, this attitude. Go deep into the world of computers. is also a kind of happiness.

Using go to develop a simple reverse proxy service This article also needs to be studied. Very good

---------------------------------------------------------------------------------------------the byte order of the Go language

Go

Recently, when looking at the source code of NSQ, when it was found to handle the message, it would use byte order to handle the packet, so I felt it necessary to understand the knowledge of the byte order in the TCP protocol.

byte order (byte order)

We generally think of bytes as the smallest unit of data. Of course, in fact, a byte contains 8 bit (bit = binary digit). In a 32-bit CPU, "word length" is 32 bits, which is 4 byte. In such a CPU, the memory is always read or written in 4-byte alignment, so what order is the 4-byte data stored in memory? Let's discuss it in more detail below.

The byte-order includes: big-endian and small-endian, why should it be so troublesome to classify it? For example, 255 uses binary expression is 1111 1111, plus 1 is 1 0000 0000, more than a 1 out, obviously we need to use an extra byte to hold this 1, but this 1 to be stored in the first byte or the second byte? At this time because people choose the difference, there are big-endian and small-order differences.

The so-called large-endian (big endian) means that its "most significant bit (most significant byte)" falls on a low-address storage mode. For example, when address a writes 0x0a0b0c0d, the data in memory is:

For the small byte order (little Endian) the opposite is the case, it puts the "least significant bit (least significant byte)" on the low address. For example:

For our commonly used CPU architectures, such as the INTEL,AMD CPU uses small endian, and for example the Mac OS used a Power PC that used the big endian (but now Mac OS also uses Intel's CPU).

Go Processing byte order

The code that handles the size of the end order in Go is located encoding/binary , the global variable Bigendian in the package is used to manipulate the big-endian data, Littleendian is used to manipulate the small-endian data, and the data types that correspond to the two variables have byteorder interfaces:

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

The first three methods are used to read the data, and the last three methods are used to write the data.

You may notice that the above method operates on unsigned integers, what if we want to manipulate the signed integer type? It's simple, casting is fine, like this:

func PutInt32(b []byte, v int32) {        binary.BigEndian.PutUint32(b, uint32(v))}

To learn more about them, let's start by writing a program that observes the way go handles big-endian and small-order:

Package Mainimport ("Encoding/binary" "FMT" "unsafe") const int_size int = int (unsafe). Sizeof (0))//Determine the byte order type in our system func Systemedian () {var i int = 0x1 BS: = (*[int_size]byte) (unsafe. Pointer (&i)) if bs[0] = = 0 {fmt. PRINTLN ("System Edian is little endian")} else {fmt. PRINTLN ("System Edian is big endian")}}func Testbigendian () {//0000 0000 0000 0000 0000 0001 1111 1111 var t Estint int32 = The FMT. Printf ("%d use big endian: \ n", Testint) var testbytes []byte = make ([]byte, 4) binary. Bigendian.putuint32 (Testbytes, UInt32 (testint)) fmt. Println ("Int32 to Bytes:", testbytes) Convint: = Binary. Bigendian.uint32 (testbytes) fmt.  Printf ("bytes to Int32:%d\n\n", Convint)}func Testlittleendian () {//0000 0000 0000 0000 0000 0001 1111 1111 var Testint int32 = The FMT. Printf ("%d use little endian: \ n", Testint) var testbytes []byte = make ([]byte, 4) binary. Littleendian.putuint32 (Testbytes, UInt32 (testint)) fmt. PRintln ("Int32 to Bytes:", testbytes) Convint: = Binary. Littleendian.uint32 (testbytes) fmt. Printf ("bytes to Int32:%d\n\n", Convint)}func main () {Systemedian () fmt. Println ("") Testbigendian () Testlittleendian ()}

Results of execution:

system edian is big endian256 use big endian:int32 to bytes: [0 0 1 0]bytes to int32: 256256 use little endian:int32 to bytes: [0 1 0 0]bytes to int32: 256
Summarize

For program compatibility, when we develop a cross-server TCP service, we convert each time we send and receive data, and this is done to ensure that the code performs as expected on any computer.

byte order of "go" go language

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.