TLV format

TLV format

TLV is a way of storing data to facilitate quick parsing of that data.

Its mainly used in transffering data in binary format in network communications.Simple exa,ple is using it in Payment Controller POS where POS will act as a transmitter and Server will be acting as a receiver.

Typically, you read the type(tag), length and value and then send those datum to a processor function. This processor functions only function will be to process type X. Then, you read the next type, it’s length and value and send it to the appropriate processor.

It’s typically used as an easy way to process data without a lot of extra overhead.

T = Tag/Type , 2 Byte hex value

L = Length , 2 Byte hex value

V = Value, L ASCII characters.


The first field is the “type” of data being processed, the second field specifies the “length” of the value, the third field contains a “length” amount of data representing the value for the “type”.

Multiple pieces of data can be transmitted in the same message by appending more triplets to a previously existing message.

Each triplet is a “top level” description, there is typically no nesting of items in TLV (although you could come up with a way to do so by encoding TLV triplets in the V of another tag).

The pros of this format is that it’s a dead simple format (easy to parse, you can skip new added fields in old programs).It’s a relatively compact encoding format.It’s relatively simple to parse you can write  a basic X.690 parser in a couple of hours).The X.690 TLV has support for nested types

TLV’s biggest disadvantage is that it is not directly human readable. Note however if the data is converted to hex it is only moderately difficult to read.Further, cons are that it’s a dead simple format (no built-in hierarchy support, encodes binary poorly, data type must be known to both sides beforehand or transmitted in some custom-encoded way, very poor support for when the data changes over time are not additive but are substitutions, etc). Some of these cons are fixable, like you can encode binary to transmit it, but that’s just more work to de-encode on the other end.

We use TLV for data formatting. And if we want to send data to receiver in binary form, we prepare a TLV package that is contain Tag-Length-Value datas. For example;
Data Tag = DF 82 0A
Data Length = 03
Data Value = 30 31 32.
when we want to send it we concatenate this 3 row datas like DF 82 0A 03 30 31 32. Data packages can contain lots of datas like that.

When receiver get it, parsing package is very easy and receiver can parse all of data smoothly.

Related article:

Parser code for TLV format