javascriptnode.jsparsingtagstlv

EMVCo TLV string parser library for Javascript


I want to parse a TLV string and get the value when I give the tag value.

Sample:

000201021642333310011114235204546757034445802IN5910My Company6005Vegas6304bb02

Based on TLV rules Tag 52 will give value 5467, 57 will be 333.


Solution

  • var str = '000201021642333310011114235204546757034445802IN5910My Company6005Vegas6304bb02';
    var headerLength = 6; // assuming header length is always 6
    var header = str.substring(0, 6);
    
    var tags = [];
    
    // start after header
    var i = 6;
    while (i < str.length) {
        var tag = str.substring(i, i + 2);
        i += 2;
        var valueLength = Number(str.substring(i, i + 2));
        i += 2;
        var value = str.substring(i, i + valueLength);
        i += valueLength;
        tags.push({tag: tag, value: value});
    }