Skip to content

Instantly share code, notes, and snippets.

@yeyus
Created February 23, 2017 05:33
Show Gist options
  • Save yeyus/f0f30f0d2e94494bbf1b9c1825889cb4 to your computer and use it in GitHub Desktop.
Save yeyus/f0f30f0d2e94494bbf1b9c1825889cb4 to your computer and use it in GitHub Desktop.
Talker Alias 7bit vs 8bit encoding/decoding
package main
import "fmt"
func main() {
data := []byte{
0x01,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
}
out := make([]byte, 7)
for i := 7; i < 56; i++ {
fmt.Printf("%d, %d -> %d %d\n", i/8, (7 - (i % 8)), (i-7)/7, 6-(i%7))
movebit(data, i/8, (7 - (i % 8)), out, (i-7)/7, 6-(i%7))
}
fmt.Printf("%v\n", out)
out2 := make([]byte, 6)
var bit49 byte
fmt.Println("Now reverse the function")
for i := 7; i < 56; i++ {
if i > 7 {
fmt.Printf("%d, %d -> %d %d\n", (i-7)/7, 6-(i%7), (i/8)-1, (7 - (i % 8)))
movebit(out, (i-7)/7, 6-(i%7), out2, (i/8)-1, (7 - (i % 8)))
} else {
bit49 = (out[0] & 0x40) >> 6
}
}
fmt.Printf("%v %v\n", out2, bit49)
}
func movebit(src []byte, srcByte int, srcBit int, dst []byte, dstByte int, dstBit int) {
bit := (src[srcByte] >> uint8(srcBit)) & 1
if bit >= 1 {
dst[dstByte] |= 1 << uint8(dstBit)
} else {
dst[dstByte] &= 0 << uint8(dstBit)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment