2 Star 0 Fork 0

深圳市格麦信息技术有限公司/go4xs

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
encode.go 818 Bytes
一键复制 编辑 原始数据 按行查看 历史
Deiva Liang 提交于 2021-07-30 10:29 +08:00 . 初始化
package xs
import (
"bytes"
"encoding/binary"
)
func (command *Command) Encode(bigEndian bool) []byte {
lenOfBuf1 := len(command.Buf1)
if lenOfBuf1 > 0xff {
command.Buf1 = command.Buf1[0:0xff]
lenOfBuf1 = 0xff
}
lenOfBuf := len(command.Buf)
buf := make([]byte, 8+lenOfBuf+lenOfBuf1)
buf[0] = command.Cmd //1
buf[1] = command.Arg1 //1
buf[2] = command.Arg2 //1
buf[3] = uint8(lenOfBuf1) //1
bInt := uint32(lenOfBuf)
bytesBuff := bytes.NewBuffer([]byte{})
if bigEndian {
_ = binary.Write(bytesBuff, binary.BigEndian, bInt)
} else {
_ = binary.Write(bytesBuff, binary.LittleEndian, bInt)
}
copy(buf[4:], bytesBuff.Bytes()) //4
idx := 8
if lenOfBuf > 0 {
copy(buf[idx:], command.Buf)
idx += lenOfBuf
}
if lenOfBuf1 > 0 {
copy(buf[idx:], command.Buf1)
}
return buf
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/sosotec/go4xs.git
git@gitee.com:sosotec/go4xs.git
sosotec
go4xs
go4xs
8416b7a08da8

搜索帮助