dowaw80220 2018-12-25 19:11
浏览 138
已采纳

布尔数组到字节数组

I have function to convert a byte array to a bool array representing 0s and 1s:

func byteArrayToBoolArray(ba []byte) []bool {
    var s []bool

    for _, b := range ba {
        for _, c := range strconv.FormatUint(uint64(by), 2) {
            s = append(s, c == []rune("1")[0])
        }
    }

    return s
}

How does a function look like that does exactly the opposite, meaning converting a bool array into a byte array?

Edit: This playground give more details how my byte array may look like: https://play.golang.org/p/tEDcZv-t_0Q

ba := []byte{123, 255}
  • 写回答

1条回答 默认 最新

  • dongzhong5833 2018-12-25 20:27
    关注

    For example, boolsToBytes, the inverse (exactly the opposite) of bytesToBools,

    package main
    
    import (
        "fmt"
    )
    
    func boolsToBytes(t []bool) []byte {
        b := make([]byte, (len(t)+7)/8)
        for i, x := range t {
            if x {
                b[i/8] |= 0x80 >> uint(i%8)
            }
        }
        return b
    }
    
    func bytesToBools(b []byte) []bool {
        t := make([]bool, 8*len(b))
        for i, x := range b {
            for j := 0; j < 8; j++ {
                if (x<<uint(j))&0x80 == 0x80 {
                    t[8*i+j] = true
                }
            }
        }
        return t
    }
    
    func main() {
        b := []byte{123, 255}
        fmt.Println(b)
        t := bytesToBools(b)
        fmt.Printf("%t
    ", t)
        b = boolsToBytes(t)
        fmt.Println(b)
    }
    

    Playground: https://play.golang.org/p/IguJ_4cZKtA

    Output:

    [123 255]
    [false true true true true false true true true true true true true true true true]
    [123 255]
    

    The question provides a function and asks for an inverse (does exactly the opposite) function.

    The question function algorithm is flawed, several inputs map to the same function value. Therefore, there is no unique inverse.

    package main
    
    import (
        "fmt"
        "strconv"
    )
    
    func byteArrayToBoolArray(ba []byte) []bool {
        var s []bool
        for _, b := range ba {
            for _, c := range strconv.FormatUint(uint64(b), 2) {
                s = append(s, c == []rune("1")[0])
            }
        }
        return s
    }
    
    func main() {
        ba1 := []byte{0xF}
        fmt.Println(byteArrayToBoolArray(ba1))
        ba2 := []byte{0x3, 0x3}
        fmt.Println(byteArrayToBoolArray(ba2))
        ba3 := []byte{0x1, 0x1, 0x1, 0x1}
        fmt.Println(byteArrayToBoolArray(ba3))
    }
    

    Playground: https://play.golang.org/p/L9VsTtbkQZW

    Output:

    [true true true true]
    [true true true true]
    [true true true true]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同