Skip to content

Instantly share code, notes, and snippets.

@Donkelv
Created May 31, 2023 22:52
Show Gist options
  • Save Donkelv/c1b79d923010b5d143cb523d6dd3b389 to your computer and use it in GitHub Desktop.
Save Donkelv/c1b79d923010b5d143cb523d6dd3b389 to your computer and use it in GitHub Desktop.
Map<String, String> decodeTLV(String tlvData) {
Map<String, String> decodedTags = {};
int currentIndex = 0;
while (currentIndex < tlvData.length) {
String tag = tlvData.substring(currentIndex, currentIndex + 2);
currentIndex += 2;
if (currentIndex + 2 > tlvData.length) {
break; // Insufficient length to extract
}
int length =
int.parse(tlvData.substring(currentIndex, currentIndex + 2), radix: 16);
currentIndex += 2;
if (currentIndex + length * 2 > tlvData.length) {
break; // Insufficient length to extract
}
String value = tlvData.substring(currentIndex, currentIndex + length * 2);
currentIndex += length * 2;
String tagName = getTagName(tag);
print('Tag: $tag, Length: $length, Value: $value');
decodedTags[tagName] = value;
}
return decodedTags;
}
String getTagName(String tag) {
String tagName;
if (tag.startsWith('9')) {
tagName = '9F${tag.substring(1)}';
} else if (tag == 'FF') {
tagName = 'Response Message Template Format 2';
} else {
tagName = tag;
}
return tagName;
}
void main() {
String tlvData =
"5F2010435553544F4D45522F494E5354414E549F160F4243544553543132333435363738399F4E04616263648E1200000000000000004103440342035E031F035F25032101014F07A00000000310109F0702FF809F0D05B860AC88009F0E0500100000009F0F05B868BC98009F3901059B02E8005F280205669F4C02032F500A564953412044454249549F0607A00000000310109F21032307529F120A564953412044454249549F11005F24032401319A032305319F02060000000001009F03060000000000009F34034203005F30020226C408418745FFFFFF3957C10A40402110080003E00040C7088ED20FBE4D81F6CFC00A40402110080003E0004EC28201A0390A81BE1BC5745603A5A151420D3A4CE7BBA9038B365BA6F3FE3F3A93B6BA7C151D11306589F86A38EE272190116D4D919CC9DADADD43615FE70C4992B2317FA31E35685EA1CDC287E491E0C779252E9B0C0F42D3EBEDAD5EEB45D51583373A8B5D654B94B6E6BE93C90D156B9F355DE7ECF96F21B2B9438D1274087F9C8E07CD2BD0E69BBD8A7F7B482350AB2C12CB61F0B458677A976B006B0295057CB56457C610EAC25FE9B83E614F7C33444157352624DE9D9EEAB041034FC29AA3FE12752D0BC4159B52318BDBDE5C967E8035BD2205502710CB89E80C28FB31A14BB2B93E1E0A4493BE82953F5E1DD54DB9507A94FE395A1EC931F3795B37A0C86E874F2BFB884E061885EBC866F6B809891FCD9A6E089C148C796993DD4FA0357D755758FA6AED39E026CEA670904A9DDAAC0351A5A2BA88CA26B3CD8D38B68A3EAC1A826B086F7BEFEA578A771E25FBEBFFE6750D4D716A019746D1EA80A672538104BF57A6F3FA5B65F845E492E612B8B0C8C72DE6985B1DDC4E2CA24455E4056A1A5455EB2227BDBB1555AA29869B5F694904EB1810CC5DAC021468E013EFBD1D";
decodeTLV(tlvData);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment