/** * @Author : zou.yingbin * @Create : 2022/3/23 11:05 * @Modify : 2022/3/23 11:05 * @note : */ package client import ( "log" "strings" ) type wareInfo struct { buf []byte // 内容[报价开始(0x10):报价结束(0x11)] exchId string // 交易所id goodsCode string // 商品代码 ask string // 卖价 bid string // 买价 last string // 最新价 tm string // 时间 } func (r *wareInfo) getField(tag byte) string { for i, v := range r.buf { if v == tag { for j, d := range r.buf[i:] { if d == 0x01 || d == 0x11 { buf := r.buf[i+2 : i+j] return string(buf) } } } } return "" } func (r *wareInfo) parseField() { r.goodsCode = r.getField(0x21) r.ask = r.getField(0x4c) r.bid = r.getField(0x42) r.last = r.getField(0x24) r.tm = r.getField(0x41) r.exchId = r.getField(0x56) if r.exchId == "200" { r.goodsCode = "200" } } func (r *wareInfo) printInfo() { if r.ask == "" { return } log.Println(r.exchId, r.goodsCode, r.ask, r.bid, r.last, r.tm) } func (r *wareInfo) debugPrintAllField() { if len(r.buf) <= 2 { return } if r.buf[0] != 0x10 || r.buf[len(r.buf)-1] != 0x11 { return } type Field struct { Tag byte Value string } var sField []Field tmp := string(r.buf[2 : len(r.buf)-1]) //0x01 字段分割 strS := strings.Split(tmp, string([]byte{0x01})) for _, v := range strS { //0x02 值分割 split := strings.Split(v, string([]byte{0x02})) if len(split) != 2 { break } d := Field{ Tag: ([]byte(split[0]))[0], Value: split[1], } sField = append(sField, d) } for _, v := range sField { log.Printf("0x%0x %s\n", v.Tag, v.Value) } }