Javaer to go byte type conversion

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

In the process of server and client communication of the socket, bytes are transmitted. And we need to show and use strings, shaping and so on. At this point, we need to process the bytes and convert the byte type data into the type we need.

1, []byte and 16 binary strings

    • []byte to 16 binary string
import  ( "bytes"   "StrConv" ) //byte to 16 binary string  func  bytetohex (data []byte ) string  {buffer: = new  (bytes. Buffer) for  _, B: = range  data {s: = Strcon V.formatint (int64  (B&0  xff), " ) if  len  (s) == 1  {buffer. WriteString ( "0" )} buffer. WriteString (s)} return  buffer. String ()}  

Because we want to do the concatenation of the strings, we declare a buffer by the bytes tool and put the StrConv. Formatint formats a single byte string into the buffer stream.

    • 16 binary string goto []byte
Import ("StrConv")//16 binary string goto []byteFunc Hextobye (hexstring) []byte{length:=Len(hex)/2Slice: = make ([]byte,length) rs: = []rune (hex) forI: =0; I <length; i++ {s: =string(rs[i*2: i*2+2])value, _: = StrConv. parseint (s), -,Ten) Slice[i] =byte(value&0xFF)    }returnSlice

The array is formed by quickly turning the character into a rune type, and then the string is then spelled in two bits and two bits in the loop.
Use StrConv. parseint the 16-binary string into a 10-binary shape.
Finally, turn the shaping into a byte into a slice.

2, Byte and Unit8

Very simple, the Golang byte and the unit8 are all 8 bits, the direct conversion can be.

    • byte Turn unit8
i := uint8(b)
    • Unit8 to Byte turn
i uint8i1b := byte(i)

3, byte[] and unit

The transitions before byte and 16-bit, 32-bit, and 64-bit unsigned shaping can be manipulated using the Bigendian (high-level addressing) under the "Encoding/binary" package and the Littleendian (low address), both of which implement the Byteorder interface. Provides more than 8-bit unsigned shaping with a byte array before the conversion interface.

typeinterface {    Uint16([]byteuint16    Uint32([]byteuint32    Uint64([]byteuint64    PutUint16([]byteuint16)    PutUint32([]byteuint32)    PutUint64([]byteuint64)    string}

If we turn a byte array into a 16-bit unsigned shaping:

runningNumber := binary.BigEndian.Uint16(data[34:36])

Beginner's Note:

During []byte and uint conversion, it is determined whether the length of the]byte matches the type. Because the conversion of the two is achieved through displacement.

Encoding/binary part of the source code:

func  (Bigendian) Uint16 (b []< Span class= "Hljs-typename" >byte ) uint16  { return  uint16  (B[1 ]) | uint16  (B[0 ]) <<8 } Func  (Bigendian) PutUint16 (b []byte , v uint16 ) {B[0 ] = byte  (v >> 8 ) B[1 ] = byte  (v)}  

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.