Skip to content

Instantly share code, notes, and snippets.

@cirias
Created April 18, 2018 07:16
Show Gist options
  • Save cirias/96aa3aabfafb6e074b3cfacb456f0a2d to your computer and use it in GitHub Desktop.
Save cirias/96aa3aabfafb6e074b3cfacb456f0a2d to your computer and use it in GitHub Desktop.
Golang decode 7bit text
package main
import (
"encoding/hex"
"fmt"
)
func main() {
const sample = "54747A0E4ACF4161D00D244ED341F4329E1E02"
bs, _ := hex.DecodeString(sample)
fmt.Println(string(decode7bit(bs)))
}
func decode7bit(bs []byte) []byte {
bit7s := make([]byte, len(bs)*8/7)
for i := range bit7s {
byteStart := (i * 7) / 8
byteEnd := ((i + 1) * 7) / 8
bitStart := (i * 7) % 8
bitEnd := ((i + 1) * 7) % 8
low := (bs[byteStart] >> uint8(bitStart)) & 0x7f
high := bs[byteEnd] << uint8(8-bitEnd) >> uint8(8-bitEnd) << uint8(7-bitEnd)
bit7s[i] = low | high
}
return bit7s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment