Firstly a template asn file (PP.asn here) was created. Then it's be used to encode some text, creating a segment binary data(stored in variable Bin), then Bin was decoded based on PP.asn. We can also store binaries into a file, then read from it.
$ cat PP.asn
PP DEFINITIONS AUTOMATIC TAGS ::=
BEGIN
TGSN ::= INTEGER (0..14)
Persons ::= SEQUENCE {
name PrintableString,
location INTEGER {home(0),field(1),roving(2)},
age INTEGER OPTIONAL
}
END
$ erl
Erlang/OTP 17 [erts-6.0] [source-07b8f44] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V6.0 (abort with ^G)
1> asn1ct:compile("PP", [ber, verbose]).
Erlang ASN.1 compiler 3.0
Compiling: "PP.asn"
Options: [ber,verbose,{i,"."}]
--{generated,"PP.asn1db"}--
--{generated,"PP.hrl"}--
--{generated,"PP.erl"}--
ok
2> Rockstar = {'Persons',"Some Name",roving,50}.
{'Persons',"Some Name",roving,50}
3> {ok,Bin} = 'PP':encode('Persons',Rockstar).
{ok,<<48,17,128,9,83,111,109,101,32,78,97,109,101,129,1,
2,130,1,50>>}
4> {ok,Persons} = 'PP':decode('Persons',Bin).
{ok,{'Persons',"Some Name",roving,50}}
5> file:write_file("test", Bin).
ok
6> {ok, Bin2} = file:read_file("test").
{ok,<<48,17,128,9,83,111,109,101,32,78,97,109,101,129,1,
2,130,1,50>>}
7> {ok,Persons} = 'PP':decode('Persons',Bin2).
{ok,{'Persons',"Some Name",roving,50}}