2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-07-11 18:58:31 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gbinary
|
|
|
|
|
2020-05-27 11:26:05 +08:00
|
|
|
// NOTE: THIS IS AN EXPERIMENTAL FEATURE!
|
2019-07-11 18:58:31 +08:00
|
|
|
|
2021-11-16 19:29:02 +08:00
|
|
|
// Bit Binary bit (0 | 1)
|
2019-07-11 18:58:31 +08:00
|
|
|
type Bit int8
|
|
|
|
|
2021-11-16 19:29:02 +08:00
|
|
|
// EncodeBits does encode bits return bits Default coding
|
2019-07-11 18:58:31 +08:00
|
|
|
func EncodeBits(bits []Bit, i int, l int) []Bit {
|
|
|
|
return EncodeBitsWithUint(bits, uint(i), l)
|
|
|
|
}
|
|
|
|
|
2021-11-16 19:29:02 +08:00
|
|
|
// EncodeBitsWithUint . Merge ui bitwise into the bits array and occupy the length bits
|
|
|
|
// (Note: binary 0 | 1 digits are stored in the uis array)
|
2019-07-11 18:58:31 +08:00
|
|
|
func EncodeBitsWithUint(bits []Bit, ui uint, l int) []Bit {
|
|
|
|
a := make([]Bit, l)
|
|
|
|
for i := l - 1; i >= 0; i-- {
|
|
|
|
a[i] = Bit(ui & 1)
|
|
|
|
ui >>= 1
|
|
|
|
}
|
|
|
|
if bits != nil {
|
|
|
|
return append(bits, a...)
|
|
|
|
}
|
2021-11-13 23:23:55 +08:00
|
|
|
return a
|
2019-07-11 18:58:31 +08:00
|
|
|
}
|
|
|
|
|
2021-11-16 19:29:02 +08:00
|
|
|
// EncodeBitsToBytes . does encode bits to bytes
|
2021-11-13 23:23:55 +08:00
|
|
|
// Convert bits to [] byte, encode from left to right, and add less than 1 byte from 0 to the end.
|
2019-07-11 18:58:31 +08:00
|
|
|
func EncodeBitsToBytes(bits []Bit) []byte {
|
|
|
|
if len(bits)%8 != 0 {
|
|
|
|
for i := 0; i < len(bits)%8; i++ {
|
|
|
|
bits = append(bits, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b := make([]byte, 0)
|
|
|
|
for i := 0; i < len(bits); i += 8 {
|
|
|
|
b = append(b, byte(DecodeBitsToUint(bits[i:i+8])))
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2021-11-16 19:29:02 +08:00
|
|
|
// DecodeBits .does decode bits to int
|
2021-11-13 23:23:55 +08:00
|
|
|
// Resolve to int
|
2019-07-11 18:58:31 +08:00
|
|
|
func DecodeBits(bits []Bit) int {
|
2020-02-26 23:26:24 +08:00
|
|
|
v := 0
|
2019-07-11 18:58:31 +08:00
|
|
|
for _, i := range bits {
|
|
|
|
v = v<<1 | int(i)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-11-16 19:29:02 +08:00
|
|
|
// DecodeBitsToUint .Resolve to uint
|
2019-07-11 18:58:31 +08:00
|
|
|
func DecodeBitsToUint(bits []Bit) uint {
|
|
|
|
v := uint(0)
|
|
|
|
for _, i := range bits {
|
|
|
|
v = v<<1 | uint(i)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-11-16 19:29:02 +08:00
|
|
|
// DecodeBytesToBits .Parsing [] byte into character array [] uint8
|
2019-07-11 18:58:31 +08:00
|
|
|
func DecodeBytesToBits(bs []byte) []Bit {
|
|
|
|
bits := make([]Bit, 0)
|
|
|
|
for _, b := range bs {
|
|
|
|
bits = EncodeBitsWithUint(bits, uint(b), 8)
|
|
|
|
}
|
|
|
|
return bits
|
|
|
|
}
|