最近处理tcp发包,各种数据需要转为unit8,然后写入tcp数据包。
string转uint16,然后unint16写入数据包
//string转uint16,然后uint16写入数据包,通过移位操作写入uint8的list
vvint16, _ := strconv.ParseUint(i.Value, 10, 16)
cmsgstr[index] = uint8(vvint16)
vvint16 >>= 8
cmsgstr[index + 1] = uint8(vvint16)
string转浮点,然后浮点转uint32,4次移位操作,写入uint8的list
//string转浮点,然后浮点转uint32,写入数据包
float, _ := strconv.ParseFloat(i.Value,32)
fmt.Printf("float:%f\n", float32(float))
cdint32 := math.Float32bits(float32(float))
fmt.Printf("转换为uint32:%x\n", cdint32)
for i:=0;i<4;i++{
cmsgstr[index + i] = uint8(cdint32)
cdint32 >>= 8
}
网友评论