Go   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了golang(Go语言) byte/[]byte 与 二进制形式字符串 互转大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_607_0@

效果

把某个字节或字节数组转换成字符串01的形式,一个字节用8个”0”或”1”字符表示。比如:
byte(3) –> “00000011”
[]byte{1,2,3} –> “[00000001 00000010 00000011]”
“[00000011 10000000]” –> []byte{0x3,0x80}

开源库 biu

实际上我已经将其封装到一个开源库了(biu),其中的一个功能就能达到上述效果

//byte/[]byte -> String
bs := []byte{1, 2, 3}
s := biu.bytesToBinaryString(bs)
fmt.Println(s) //[00000001 00000010 00000011]
fmt.Println(biu.byteToBinaryString(byte(3))) //00000011

//String -> []byte
s := "[00000011 10000000]"
bs := biu.binaryStringToBytes(s)
fmt.Printf("%#v\n",bs) //[]byte{0x3,0x80}

代码实现

const (
    zero  = byte('0')
    one   = byte('1')
    lsb   = byte('[') // left square brackets
    rsb   = byte(']') // right square brackets
    space = byte(' ')
)

var uint8arr [8]uint8

// ErrBadStringFormat represents a error of input String's format 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] = 128
    uint8arr[1] = 64
    uint8arr[2] = 32
    uint8arr[3] = 16
    uint8arr[4] = 8
    uint8arr[5] = 4
    uint8arr[6] = 2
    uint8arr[7] = 1
}

// append bytes of String in binary format.
func appendBinaryString(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 = append(bs,onE)
        }
        b <<= 1
    }
    return bs
}


// ByteToBinaryString get the String in binary format of a byte or uint8.
func ByteToBinaryString(b byte) String {
    buf := @H_817_149@make([]byte, 0, 8)
    buf = appendBinaryString(buf,b)
    return String(buf)
}

// BytesToBinaryString get the String in binary format of a []byte or []int8.
func BytesToBinaryString(bs []byte) String {
    l := len(bs)
    bl := l*8 + l + 1
    buf := @H_817_149@make([]byte,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 is going to be in binary format.
var rbDel = regexp.MustCompile(`[^01]`)

// BinaryStringToBytes get the binary bytes according to the
// input String which is in binary format.
func BinaryStringToBytes(s String) (bs []byte) {
    if len(s) == 0 {
        panic(ErrEmptyString)
    }

    s = rbDel.replaceAllString(s,"")
    l := len(s)
    if l == 0 {
        panic(ErrBadStringFormat)
    }

    mo := l % 8
    l /= 8
    if mo != 0 {
        L++
    }
    bs = @H_817_149@make([]byte,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
}

大佬总结

以上是大佬教程为你收集整理的golang(Go语言) byte/[]byte 与 二进制形式字符串 互转全部内容,希望文章能够帮你解决golang(Go语言) byte/[]byte 与 二进制形式字符串 互转所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。