I'm new to Java, so I would like to use the standard solution for, I think, the standard task. The length of tags and values ββare not known.
Asked
Active
Viewed 1.7k times
7 Answers
6
You can use this BER-TLV parser: source code on git.
Examples:
How to parse
byte[] bytes = HexUtil.parseHex("50045649534157131000023100000033D44122011003400000481F");
BerTlvParser parser = new BerTlvParser(LOG);
BerTlvs tlvs = parser.parse(bytes, 0, bytes.length);
How to build
byte[] bytes = new BerTlvBuilder()
.addHex(new BerTag(0x50), "56495341")
.addHex(new BerTag(0x57), "1000023100000033D44122011003400000481F")
.buildArray();
Maven dependency
<dependency>
<groupId>com.payneteasy</groupId>
<artifactId>ber-tlv</artifactId>
<version>1.0-10</version>
</dependency>

Maksym
- 2,650
- 3
- 32
- 50
1
Example C# code for bertTlv decode
public class Tlv : ITlv, IFile
{
List<TlvModel> modelList=new();
string parser = "";
string length = "";
String empty = "";
string ascii = "";
int decValue = 0;
int step = 0;
public Tlv(String data)
{
TlvParser(data.Replace(" ",""));
}
public void readTag()
{
String line = "";
StreamReader sr = new StreamReader("taglist.txt");
while ((line = sr.ReadLine()) != null)
{
modelList.Add(new()
{
tag = line.Split(",")[0].Trim(),
description = line.Split(",")[1].Trim()
});
}
sr.Close();
}
public void insertTag()
{
try
{
StreamWriter sw = new StreamWriter("test.txt");
foreach (var item in modelList)
{
sw.WriteLine($"{item.tag},{item.description}");
}
sw.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
}
public int writeFile(String parser)
{
StreamWriter sw = new StreamWriter("output.txt");
sw.WriteLine(parser);
sw.Close();
return 0;
}
private int TlvParser(String data, int i = 0, string tag = "")
{
if (i == 0)
{
readTag();
}
if (i < data.Length)
{
tag += data[i];
TlvModel model = getTag(tag);
if (model != null)
{
decValue = int.Parse(data.Substring(i + 1, 2), System.Globalization.NumberStyles.HexNumber);
// lengthControl(data,i+3,decValue);
if (model.description.Contains("Template"))
{
parser += $"{empty}|------ tag: {model.tag}({model.description})\n";
step += 1;
empty = Empty();
return TlvParser(data, i + 3, "");
}
else
{
parser += $"{empty}|------ tag: {model.tag}({model.description}){empty}|------ value --> {ConvertHex(data.Substring(i + 3, decValue * 2))} \n";
}
i += 3 + decValue * 2;
return TlvParser(data, i, "");
}
else
{
return TlvParser(data, i + 1, tag);
}
}
return writeFile(parser);
}
public TlvModel getTag(string tag)
{
return modelList.Find(i => i.tag == tag);
}
public string ConvertHex(string hex)
{
ascii = "";
for (int i = 0; i < hex.Length; i += 2)
{
ascii += System.Convert.ToChar(System.Convert.ToUInt32(hex.Substring(i, 2), 16));
}
return ascii;
}
private string Empty()
{
for (int s = 0; s < step; s++)
{
empty += "\t";
}
return empty;
}
public void setTag(TlvModel model)
{
modelList.Add(model);
insertTag();
}
}

Umit KOC
- 224
- 2
- 6
0
I made a simple parser based on the information provided here: http://www.codeproject.com/Articles/669147/Simple-TLV-Parser
I don't know if this code support all the standard, but it works for me.
public static Map<String, String> parseTLV(String tlv) {
if (tlv == null || tlv.length()%2!=0) {
throw new RuntimeException("Invalid tlv, null or odd length");
}
HashMap<String, String> hashMap = new HashMap<String, String>();
for (int i=0; i<tlv.length();) {
try {
String key = tlv.substring(i, i=i+2);
if ((Integer.parseInt(key,16) & 0x1F) == 0x1F) {
// extra byte for TAG field
key += tlv.substring(i, i=i+2);
}
String len = tlv.substring(i, i=i+2);
int length = Integer.parseInt(len,16);
if (length > 127) {
// more than 1 byte for lenth
int bytesLength = length-128;
len = tlv.substring(i, i=i+(bytesLength*2));
length = Integer.parseInt(len,16);
}
length*=2;
String value = tlv.substring(i, i=i+length);
//System.out.println(key+" = "+value);
hashMap.put(key, value);
} catch (NumberFormatException e) {
throw new RuntimeException("Error parsing number",e);
} catch (IndexOutOfBoundsException e) {
throw new RuntimeException("Error processing field",e);
}
}
return hashMap;
}

Ignacio A. Poletti
- 616
- 4
- 7
-
2simple-tlv is different from ber-tlv, and a simple tlv parser will often crash on ber-tvl data β K.L. Nov 26 '14 at 13:05
0
Might be this free library can be useful for you. I've used this one for simple TLV parsing. Anyway it's with MIT license and you can modify it.
https://github.com/VakhoQ/tlv-encoder

grep
- 5,465
- 12
- 60
- 112