Golang (Go language) byte/[]byte and binary form string mutual transfer

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

Effect

Converts a byte or byte array into the form of a string 01, with a byte represented by 8 "0" or "1" characters. Like what:
BYTE (3)--"00000011"
[]byte{1,2,3}-"[00000001 00000010 00000011]"
"[00000011 10000000]"--[]byte{0x3, 0x80}

Open Source Library Biu

In fact, I've encapsulated it in an open source library (Biu), one of these features:

//byte/[]byte -> stringbs := []byte{1, 2, 3}s := biu.BytesToBinaryString(bs)fmt.Println(s) //[00000001 00000010 00000011]fmt.Println(biu.ByteToBinaryString(byte(3))) //00000011//string -> []bytes := "[00000011 10000000]"bs := biu.BinaryStringToBytes(s)fmt.Printf("%#v\n", bs) //[]byte{0x3, 0x80}

Code implementation

Const (zero = byte (' 0 ') one = Byte (' 1 ') LSB = Byte (' [')//left square brackets RSB = byte ('] ')/rig HT square brackets space = byte (")) var Uint8arr [8]uint8//Errbadstringformat represents a error of input string ' s fo Rmat is illegal. var errbadstringformat = errors. New ("Bad string format")//erremptystring represents a error of empty input String.var erremptystring = errors. New ("empty string") Func init () {uint8arr[0] = uint8arr[1] = uint8arr[2] = uint8arr[3] = uint8 ARR[4] = 8 uint8arr[5] = 4 Uint8arr[6] = 2 uint8arr[7] = 1}//Append bytes of string in binary Format.func append        Binarystring (BS []byte, B byte) []byte {var a byte for I: = 0; i < 8; i++ {a = b b <<= 1 b >>= 1 Switch A {case b:bs = append (BS, zero) Default:bs = Appen D (BS, one)} b <<= 1} return bs}//bytetobinarystring get the string in binary foRmat of a byte or Uint8.func bytetobinarystring (b byte) string {buf: = make ([]byte, 0, 8) buf = Appendbinarystring ( BUF, B) return string (BUF)}//bytestobinarystring get the string in binary format of a []byte or []int8.func bytestobin     Arystring (BS []byte) string {l: = Len (bs) BL: = l*8 + L + 1 buf: = Make ([]byte, 0, bl) buf = append (buf, LSB)     For _, B: = range bs {buf = appendbinarystring (buf, b) buf = append (buf, Space)} BUF[BL-1] = RSB return string (BUF)}//regex for delete useless string which are going to being in binary format.var Rbdel = RegExp. Mustcompile (' [^01] ')//binarystringtobytes get the binary bytes according to the//input string which are in binary format. Func binarystringtobytes (S string) (BS []byte) {if Len (s) = = 0 {panic (erremptystring)} s = Rbdel.replac  Eallstring (S, "") L: = Len (s) if L = = 0 {panic (errbadstringformat)} MO: = l% 8 L/= 8 if mo! = 0 {l++} bs = maKe ([]byte, 0, l) mo = 8-mo var n uint8 for I, B: = range []byte (s) {m: = (i + MO)% 8 Switch B {        Case One:n + = Uint8arr[m]} if M = = 7 {bs = append (BS, n) n = 0 }} return}
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.