项目作者: asxalex

项目描述 :
bits for go
高级语言: Go
项目地址: git://github.com/asxalex/gobinary.git
创建时间: 2019-07-15T06:38:18Z
项目社区:https://github.com/asxalex/gobinary

开源协议:

下载


gobinary

package for binary header in go.

install

go get github.com/asxalex/gobinary

usage

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/asxalex/gobinary"
  5. )
  6. func main() {
  7. header := gobinary.NewBinaryHeader()
  8. header.AddField("version", 2) // 2 bit for version
  9. header.AddField("MF", 1) // 1 bit for M(ore)F(ragment)
  10. header.AddField("reserve1", 5) // 5 bit for reservation
  11. header.SetBitValue("version", 1) // set the version field to 1
  12. header.SetBitValue("MF", 1) // set the MF to 1
  13. bin := header.ToBinary() // get the binary according to the header
  14. fmt.Println(bin)
  15. // set the fields "versiong", "MF" and "reserve1"
  16. // accroding to the binary 0x80, that's to say, the
  17. // version field is set to 0b10, MF to 0b0, and reserve1 to 0b00000
  18. header.FromBinary([]byte{0x80})
  19. }

test and benchmark

  1. $ go test -v -bench=.
  2. === RUN TestBinary
  3. --- PASS: TestBinary (0.00s)
  4. goos: darwin
  5. goarch: amd64
  6. pkg: gobinary
  7. BenchmarkBinaryConversion-4 5000000 362 ns/op
  8. PASS
  9. ok gobinary 2.189s

License

BSD