tlvEncode method
Encodes a list of TLV objects into a TLV encoded string.
This method processes a list of TLV objects and converts each object into its corresponding TLV format by concatenating the tag, length (as a 2-digit number), and the value. It returns the concatenated string representing the encoded TLV data.
-
Parameters:
tlvList
: A list of TLV objects that need to be encoded into the TLV format.
-
Returns: A string representing the encoded TLV data, with each tag-length-value pair concatenated together.
Example:
TLVService tlvService = TLVService();
List<TLV> tlvList = [
TLV("00", 12, "345A"),
TLV("01", 3, "ABC")
];
String encoded = tlvService.tlvEncode(tlvList);
print(encoded); // "0012345A0103ABC"
Implementation
String tlvEncode(List<TLV> tlvList) {
StringBuffer encodedData = StringBuffer();
for (var tlv in tlvList) {
String length = tlv.length.toString().padLeft(2, '0');
encodedData.write(tlv.tag);
encodedData.write(length);
encodedData.write(tlv.value);
}
return encodedData.toString();
}