1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package main
- import (
- "encoding/hex"
- "fmt"
- "testing"
- )
- func Test_verify(t *testing.T) {
- //Name := "光传感器"
- //decodeAddress, _ := hex.DecodeString("01")
- //dataPack := envSensor.DataPack{Address: decodeAddress, Start: []byte{0x01, 0xF8}, DataLen: []byte{0x00, 0x08}}
- //
- //if Name == "光传感器" {
- // dataPack = envSensor.DataPack{Address: decodeAddress, Start: []byte{0x00, 0x06}, DataLen: []byte{0x00, 0x01}}
- //}
- //buffer := dataPack.GetEnvGatherCommand()
- //fmt.Println("++++++++", hex.EncodeToString(buffer.Bytes()))
- //
- //xxx := "010310030900ba012c000000000000000100016324" //42 14
- //
- //gz, _ := strconv.ParseInt(xxx[30:38], 16, 0)
- ////s := xxx[30:38]
- //fmt.Println("截取: ", gz)
- //hexStr := "7830303940790d0a"
- //// 调用解析函数
- //
- //hex.EncodeToString([]byte{})
- //result := parseHexString(hexStr)
- //// 输出结果
- //fmt.Println(result)
- //xxx := "7830313940790d0a"
- //char := parseHexString("7830313940790d0a")
- //fmt.Println("+++++++", char)
- //fmt.Println("len++", len(xxx))
- //fmt.Println(",,,", xxx[0:2], xxx[8:16])
- //dataPack := envSensor.DataPack{Address: "02", Start: []byte{0x00, 0x06}, DataLen: []byte{0x00, 0x01}}
- //command := dataPack.GetEnvGatherCommand()
- //fmt.Println("+++++", hex.EncodeToString(command.Bytes()))
- //updates := map[string]interface{}{
- // "id": 1,
- // "startTime": "00:30",
- // "endTime": "00:20",
- //}
- //err := config.UpdateYAMLConfig(updates)
- //if err != nil {
- // fmt.Println(err)
- //}
- //pack := switchRelay.DataPack{Address: "fe", Way: 2, Command: 0}
- //command := pack.GetSwitchRelayCommand()
- //fmt.Println("MingLing。。。", hex.EncodeToString(command.Bytes()))
- ints := []int{1, 2, 3}
- state := generateHexSwitchState(ints)
- fmt.Println("转换后:", hex.EncodeToString(state))
- }
- func parseHexString(hexStr string) string {
- // 将十六进制字符串转换为字节数组
- bytes, err := hex.DecodeString(hexStr)
- if err != nil {
- fmt.Println("错误: 十六进制字符串解析失败", err)
- return ""
- }
- // 将字节数组转换为对应的字符
- return string(bytes)
- }
- func generateHexSwitchState(switches []int) []byte {
- var state int
- // 遍历切片,设置对应位置的开关为 1
- for _, s := range switches {
- state |= 1 << (s - 1) // 设置第s位为1,s从1开始
- }
- // 返回字节切片,使用 []byte{} 来包裹十六进制值
- return []byte{byte(state)}
- }
- // UpdateYAMLConfig 用于更新 YAML 配置文件中的多个值
|