股票市场HEX字符串解码异常排查:结果与预期不符求助
股票市场HEX字符串解码错误排查
待解码的HEX字符串:
34 00 01 31 24 05 00 00 2C 00 41 50 DE C1 28 05 00 00 00 C4 C9 67 02 42 64 00 00 00 45 41 42 4C 2E 4F 30 30 30 30 20 20 50 62 25 00 00 01 20 10 27 00 80 00
文档定义的字段规则
- Length:偏移0,长度2,小端(Little-Endian)编码的16位无符号整数
- MessageType:偏移2,长度1,单个ASCII字符字节
- Nanosecond:偏移3,长度4,小端编码的32位无符号整数
- Order ID:偏移7,长度8,小端编码的64位无符号整数
- Side:偏移15,长度1,单个ASCII字符字节
- Quantity:偏移16,长度4,小端编码的32位无符号整数
- Symbol:偏移20,长度12,左对齐、右侧补空格的标准ASCII字符
- Price:偏移32,长度4,带四位隐含小数的小端编码有符号32位整数
- Flags:偏移36,长度1,最多8个1位标志的字节(0位为最低有效位)
- Sub Book:偏移37,长度1,8位无符号整数
- SettlementType:偏移38,长度1,单个ASCII字符字节
- Interest Rate:偏移39,长度4,带四位隐含小数的小端编码有符号32位整数
- Term:偏移43,长度1,8位无符号整数
我的Node.js解码代码
hexString = hexString.replace(/\s/g, '') // Convert the hex pairs to a buffer let buffer = Buffer.from(hexString, 'hex'); // Extract the various fields from the buffer let length = buffer.readUInt16LE(0); let messageType = buffer.toString('ascii', 2, 3); let nanosecond = buffer.readUInt32LE(3); let orderId = buffer.readBigUInt64LE (7); let side = buffer.toString('ascii', 15, 16); let quantity = buffer.readUInt32LE(16); let symbol = buffer.toString('ascii', 20, 32).replace(/ /g, ''); let price = buffer.readInt32LE(32) / 10000.0; let flags = buffer.toString('ascii', 36, 37); let subBook = buffer.toString('ascii', 37, 38); let settlementType = buffer.toString('ascii', 38, 39); let interestRate = buffer.readInt32LE(39) / 10000.0; let term = buffer.toString('ascii', 43, 44);
实际解码结果
> Length: 52 > MessageType: ☺ > Nanosecond: 336945 > Order ID: 2936872868291554304 > Side: ♣ > Quantity: 3288334336 > Symbol: Ig☻BdEABL > Price: 80847.2366 > Flags: 0 > Sub Book: 0 > SettlementType: > Interest Rate: 62720.0032 > Term:
预期输出结果
Length: 52 MessageType: "4" Nanosecond: 588124 Order ID: 739452273724920 Side: "1" Quantity: 100 Symbol: "EABL.O0000 Pb" Price: 25.0 Flags: " " Sub Book: " " SettlementType: " " Interest Rate: 0.0 Term: " "
问题
我首次处理此类数据,实际解码结果与预期差异极大,想请教问题出在哪?是解码逻辑错误、文档理解偏差还是编码器问题?烦请帮忙排查。
内容的提问来源于stack exchange,提问作者Kinyua James
相关产品推荐
相关产品推荐

