Golang--Network byte codec (1)

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

In the process of network transport protocol, the common way of packet encapsulation is generally:
① Header ID + data header (Type/attribute/data length) + Data body + Tail ID--generally also need to be escaped
② fixed Length--codec convenient, waste broadband
③ by the end mark (eg. transfer via base64, ends with a)--codec convenient, waste Broadband


The following are the common means of Golang decoding

    • Decoding
      The first thing you need to do is convert the []byte ASCII string to Uint8,uint16,uint32,uint64, which corresponds to Python's b,h,i,q, respectively]. Pay special attention to big-endian and small-end alignment (python big-endian ">", "Small End" < ", Also use their ASCII bar here)

  This is an example of,  decoding out   ">hhi" Func unpack (data []byte, edian byte)   (UInt16,  uint16, uint32, []byte)  {    datatype := touint16 (Data[0:2] ,  edian)     dataattr := touint16 (Data[2:4], edian)      datalen := touint32 (Data[4:8], edian)     extra := data[8:]     return datatype, dataattr, datalen, extra}func touint8 (buf  []byte, edian byte)  uint8 {    // len (BUF)  == 1     -->b    t := uint8 (buf[0])      Return t}func touint16 (Buf []byte, edian byte)  uint16 {     // len (BUF)  == 2    -->H    t :=  UInt16 (buf[0])     if edian == 62 { //  ">"          t = t<<8 | uint16 (buf[1])     } else if  edian == 60 { //  "<"         t =  T | uint16 (buf[1]) <<8    }    return t}func  ToUInt32 (Buf []byte, edian byte)  uint32 {    // len (BUF)  == 4    -->i    t := uint32 (Buf[0])      if edian == 62 {        t =  t << 24        t = t |  UInt32 (Buf[1]) <<16        t = t | uint32 (buf [2]) <<8  &nbsP;     t = t | uint32 (buf[3])     } else  if edian == 60 {        t = t  | uint32 (buf[1]) <<8        t = t |  uint32 (buf[2]) <<16        t = t |  UInt32 (Buf[3]) <<24    }    return t}func touint64 (buf  []byte, edian byte)  uint64 {    //len (BUF)  == 8     -->q    t := uint64 (buf[0])     if  edian == 62 {        t = t <<  56        t = t | uint64 (buf[1]) <<48      &nbSp;  t = t | uint64 (buf[2]) <<40         t = t | uint64 (buf[3]) <<32        t  = t | uint64 (Buf[4]) <<24        t =  t | uint64 (buf[5]) <<16        t = t  | uint64 (Buf[6]) <<8        t = t |  uint64 (Buf[7])     } else if edian == 60 {         t = t | uint64 (buf[1]) <<8         t = t | uint64 (buf[2]) <<16         t = t | uint64 (buf[3]) <<24         t = t  | uint64 (buf[4]) <<32        t = t |  UInt64 (Buf[5]) <<40        t = t | uint64 (buf [6]) <<48        t = t | uint64 (Buf[7]) <<56     }    return t}

    • Coding
      Decoding the inverse of the process, the uint8,uint16,uint32 into a []byte-->ascii slice

  This is an example, coded as ">hhi" Func pack (datatype uint16, dataattr uint16, datalen  UInt32)  []byte {    buf := make ([]byte, 8)      putuint16 (datatype, buf[0:2], 62)     putuint16 (DataAttr, buf[2:4],  62)     putuint32 (datalen, buf[4:8], 62)     //buf  = buftype + bufattr + buflen    return buf}func  PutUInt8 (Num uint8, buf []byte, edian byte)  {    // len (BUF)  == 1    buf[0] = byte (num)}func putuint16 (num uint16,  buf []byte, edian byte)  {    // len (BUF)  == 2     buf[0] = byte (num >> 8)     buf[1] =  byte (num)     if edian == 62 { //  ">"     } else if  edian == 60 { //  "<"         buf[0]  ^= buf[1]        buf[1] ^= buf[0]         buf[0] ^= buf[1]    }}func putuint32 ( Num uint32, buf []byte, edian byte)  {    // len (BUF)  == 4    buf[0] = byte (num >> 24)      buf[1] = byte (num >> 16)     buf[2] = byte (num &NBSP;&GT;&GT;&NBSP;8)     buf[3] = byte (num)     if  edian == 62 {    } else if edian == 60 {       &nbSP;&NBSP;BUF[0]&NBSP;^=&NBSP;BUF[3]&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;BUF[3]&NBSP;^=&NBSP;BUF[0]         buf[0] ^= buf[3]         buf[1] ^= buf[2]        buf[2] ^= buf[ 1]        buf[1] ^= buf[2]    }}func  putuint64 (Num uint64, buf []byte, edian byte)  {    //  len (BUF)  == 8    if edian == 62 {         buf[0] = byte (num >> 56)          buf[1] = byte (num >> 48)          buf[2] = byte (num >> 40)         buf[3]  = byte (nUM&NBSP;&GT;&GT;&NBSP;32)         buf[4] = byte (num > > 24)         buf[5] = byte (num >> 16 )         buf[6] = byte (num >> 8)          buf[7] = byte (num)     } else if  edian == 60 {        buf[0] = byte (num)         buf[1] = byte (num >> 8)          buf[2] = byte (num >> 16)          buf[3] = byte (num >> 24)          buf[4] = byte (num >> 32)          buf[5] =&Nbsp;byte (num >> 40)         buf[6] = byte ( num >> 48)         buf[7] = byte (num  >> 56)     }}


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.