Last active
March 6, 2023 02:12
-
-
Save JackyCZJ/f41688cd81af93d4b05204ab77c39581 to your computer and use it in GitHub Desktop.
AT+QCFG Band Calculation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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