Skip to content

Instantly share code, notes, and snippets.

@JackyCZJ
Last active March 6, 2023 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JackyCZJ/f41688cd81af93d4b05204ab77c39581 to your computer and use it in GitHub Desktop.
Save JackyCZJ/f41688cd81af93d4b05204ab77c39581 to your computer and use it in GitHub Desktop.
AT+QCFG Band Calculation
package test
import (
"math/big"
"reflect"
"strconv"
"testing"
)
//How to calculate the band mask greater than 64 for AT+QCFG="band"
//The band mask is a 128-bit number, each bit represents a band.
// For golang , the bit > 64 need to use big.Int
func BandMask(n int) big.Int {
mask := new(big.Int).Exp(big.NewInt(2), big.NewInt(int64(n-1)), nil)
return *mask
}
func CombineBand(bands []string) string {
var b big.Int
for _, v := range bands {
n, err := strconv.Atoi(v)
if err != nil {
continue
}
bm := BandMask(n)
b = *new(big.Int).Add(&b, &bm)
}
return b.Text(16)
}
func ResolveBand(band string) []string {
b := big.NewInt(0)
b.SetString(band, 16)
var bands []string
for i := 0; i < 128; i++ {
bi := BandMask(i)
bband := new(big.Int).And(&bi, b)
if bband.String() != "0" && i != 0 {
bands = append(bands, strconv.Itoa(i))
}
}
return bands
}
func Test(t *testing.T) {
bands := []string{"1", "2", "3", "4", "5", "7", "8", "12", "13", "14", "17", "18", "19", "20", "25", "26", "28", "29", "30", "32", "34", "38", "39", "40", "41", "42", "43", "46", "48", "49", "66", "71"}
resolveBands := ResolveBand(CombineBand(bands))
if reflect.DeepEqual(bands, resolveBands) {
t.Log("success")
} else {
t.Error("failed")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment