hi there i need to parse the byte data of a response from api the data is
[0, 2, 0, 44, 0, 6, 58, 1, 0, 1, 109, 85, 0, 0, 0, 1, 0, 1, 111, 97, 0, 115, 224, 4, 0, 0, 0, 0, 0, 0, 1, 114, 0, 1, 115, 169, 0, 1, 116, 18, 0, 1, 108, 121, 0, 1, 113, 241, 0, 44, 0, 13, 128, 1, 0, 0, 55, 200, 0, 0, 0, 10, 0, 0, 55, 227, 5, 172, 149, 3, 0, 0, 84, 154, 0, 0, 0, 0, 0, 0, 56, 79, 0, 0, 57, 28, 0, 0, 54, 226, 0, 0, 56, 89]
there are certain rules to parse the data according to api provider as below
A The first two bytes ([0 - 2] -- SHORT or int16) represent the number of packets in the message.
B The next two bytes ([2 - 4] -- SHORT or int16) represent the length (number of bytes) of the first packet.
C The next series of bytes ([4 - 4+B]) is the quote packet.
D The next two bytes ([4+B - 4+B+2] -- SHORT or int16) represent the length (number of bytes) of the second packet.
C The next series of bytes ([4+B+2 - 4+B+2+D]) is the next quote packet.
please someone help me how to parse this data according to these rules in dart. i am stuck thanks for help
First, you need to convert the list of integers into a list of bytes:
final byteList = Uint8List.fromList(responseList);
Then you need to create a ByteData
from that byte list:
final byteData = ByteData.view(byteList.buffer);
Then you can do your parsing of various bytes, shorts, ints, or longs that you want at various byte offsets. For example:
final packetNum = byteData.getUint16(0);
final firstPacketLength = byteData.getUint16(2);
final firstPacketView = ByteData.sublistView(byteData, 4, 4 + firstPacketLength);
// Do whatever you need for that packet
final secondPacketPos = 4 + firstPacketLength;
final secondPacketLength = byteData.getUint16(secondPacketPos),
final secondPacketView = ByteData.sublistView(byteData, secondPacketPos + 2, 4 + secondPacketLength);
// Do whatever you need for the saecond packet
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.