ber-tlv icon indicating copy to clipboard operation
ber-tlv copied to clipboard

Add a way to get the byte representation of constructed tags

Open martinpaljak opened this issue 7 years ago • 7 comments

martinpaljak avatar Oct 24 '17 06:10 martinpaljak

Hi,

You can use BerTlvBuilder for this. For example,

BerTlv constructedTag = new BerTlv(new BerTag(0xE4)
        , Arrays.asList(new BerTlv(new BerTag(0x86), HexUtil.parseHex("ED3C3B8B03928D0E0012")))
);

BerTlvBuilder builder = new BerTlvBuilder();
builder.addBerTlv(constructedTag);
byte[] bytes = builder.buildArray();

System.out.println("bytes = " + HexUtil.toFormattedHexString(bytes));

evsinev avatar Oct 26 '17 10:10 evsinev

OK, but maybe it would make sense to do this transparently, rather than throw when calling getBytes on a constructed tag?

martinpaljak avatar Nov 13 '17 06:11 martinpaljak

@martinpaljak If we add getBytes() to constructed tags what should we do with other methods: getHexValue(), getTextValue(), getIntValue()? I've added this throw to prevent my errors when working with constructed/primitive tags.

evsinev avatar Nov 13 '17 18:11 evsinev

Some options:

  1. as the name implies, getBytes does not refer to value, rather "encoding". It would be OK to let the *Value methods to keep throwing
  2. Have a new method getByteEncoding()
  3. ... ?

martinpaljak avatar Nov 13 '17 21:11 martinpaljak

May be it would be better to create another method only for a constructed tag: getConstructedBytes()?

evsinev avatar Nov 13 '17 23:11 evsinev

getContentBytes() could be universal for both types, no ?

martinpaljak avatar Nov 14 '17 07:11 martinpaljak

Still getting back to this - the main reason for this is to be able to re-show the binary representation of what was parsed in a debug log, which would need to include the length of constructed tags. Would love to use your project in GlobalPlatformPro for this.

martinpaljak avatar Nov 15 '22 15:11 martinpaljak