From de673b3264c68926c693e3714c70761a3bcedf1c Mon Sep 17 00:00:00 2001 From: BiSaXa <1669855+BiSaXa@users.noreply.github.com> Date: Mon, 29 Aug 2022 03:21:51 +0300 Subject: janky bitreader, usercmdinfo almost finished --- utils/bitreader.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 utils/bitreader.go (limited to 'utils/bitreader.go') diff --git a/utils/bitreader.go b/utils/bitreader.go new file mode 100644 index 0000000..c4936fd --- /dev/null +++ b/utils/bitreader.go @@ -0,0 +1,41 @@ +package utils + +import ( + "fmt" + "math" + "strconv" +) + +func ReadBitStateLSB(input byte, index int) (bool, error) { + if index < 0 && index > 7 { + return false, fmt.Errorf("IndexOutOfBounds for type byte") + } + value := input & (1 << index) + return (value > 0), nil +} + +func Read32BitsAfterFirstBitInt32(input []byte, index int, step int) int32 { + binary := "" + binary += fmt.Sprintf("%08b", input[step])[8-index : 8] + binary += fmt.Sprintf("%08b", input[step-1]) + binary += fmt.Sprintf("%08b", input[step-2]) + binary += fmt.Sprintf("%08b", input[step-3]) + binary += fmt.Sprintf("%08b", input[step-4])[:8-index] + output, err := strconv.ParseInt(binary, 2, 32) + CheckError(err) + return int32(output) + +} + +func Read32BitsAfterFirstBitFloat32(input []byte, index int, step int) float32 { + binary := "" + binary += fmt.Sprintf("%08b", input[step])[8-index : 8] + binary += fmt.Sprintf("%08b", input[step-1]) + binary += fmt.Sprintf("%08b", input[step-2]) + binary += fmt.Sprintf("%08b", input[step-3]) + binary += fmt.Sprintf("%08b", input[step-4])[:8-index] + output, err := strconv.ParseUint(binary, 2, 32) + CheckError(err) + return math.Float32frombits(uint32(output)) + +} -- cgit v1.2.3