Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8386337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:54:32+00:00 2026-06-09T17:54:32+00:00

Yesterday I came to know about representing information using TLV format. If you were

  • 0

Yesterday I came to know about representing information using TLV format.

If you were to write a portable BER TLV encoder/decoder in ANSI C, what data structure would you use (*)?

Would something like the follwoing do?

struct TlvElement
{
    int nTag;
    int nLength;
    unsigned char *pValue; // Byte array
    TlvElement *pNext;
}; 

(*) Unfortunately I can’t use C++ and STL for this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:54:33+00:00Added an answer on June 9, 2026 at 5:54 pm

    From the wiki article:

    The type and length are fixed in size (typically 1-4 bytes)

    So, I’d change the nTag and nLength to some fixed-length type. int‘s size is platform specific and this could cause you some troubles. Fix their sizes for your protocol and use int8_t, int16_t or int32_t, etc. For nLength, you may even use unsigned.


    As the value could be any type, I’d use void* for pValue, instead of unsigned char*.


    How you will use this data structure? How you want to have access to the different TLVs?
    My point is – do you need linked list? Or, will linked list be the best option for your case/application/purposes/etc?

    What I’m trying to say is, that you may remove the pNext element and just treat the TLVs as elements of a (dynamically growing) array. This one really depends on your needs.

    Most probably, as you’re implementing TLVs, you’ll need to send them through some connection, right? If so, you need to think about some protocol. I’d do something like this – send the total numbers of TLVs at the very beginning and I would NOT use linked-list, but a dynamic array.
    You should be careful sending such data structure through the network – the pNext pointers will not be valid, they must be reset on the other side of the connection.
    You also need to carefully send the data, but I guess you know these things. I just wanted to mention them.


    EDIT I see you have some troubles understanding what does nested TLV mean.

    A nested TLV is just a TLV element, which has value of a type TLV. And this has nothing to do with the “container” of TLVs – dynamic array or linked list.

    Here’s an untested example, just to get the idea. I’d do this like this:

    struct TLV
    {
        uint32_t nTag;
        uint32_t nLength;
        void* pValue;
    };
    
    // created dynamic array with 3 TLVs:
    TLV* pMyTLVs = malloc( 3 * sizeof( struct TLV ) );
    
    // set the first 2 TLVs, some primitives, for example
    // ..
    
    // now, set the third TLV to be nested:
    pMyTLVs[ 2 ].nTag = ...; // set some tag, that will indicate nested TLV
    pMyTLVs[ 2 ].nLength = ...; // set length of the TLV element
    pMyTLVs[ 2 ].pValue = malloc( sizeof( struct TLV ) );
    
    // get pointer to the new, _nested_ TLV:
    TLV* pNested = (TLV*)pMyTLVs[ 2 ].pValue; 
    
    // now use the pNested TLV as an usual TLV:
    pNested->nTag = ...;
    pNested->nLength = ...;
    pNested->pValue = ...;
    
    // of course, pNested is not absolutely necessary, you may use directly
    // pMyTLVs[ 2 ].pValue->...;
    // but using pNested, makes the code more clear
    

    NOTE: once again, this is not tested code, but I guess you get the idea. Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across Google's Page Speed add-on for Firebug yesterday. The page about using
I was using python(x,y), which came with Spyder. Yesterday, Spyder crashed I can't figure
Yesterday i was reading some questions here on SO and i came to know
I started reading about struts2 yesterday and came across the below slide. Site link
So, C# 4.0 came out yesterday. It introduced the much-debated dynamic keyword, named and
Yesterday 2 of the guys on our team came to me with an uncommon
Yesterday, the lady that uses the products sold report came to me with an
Yesterday I began to write C# in order to develop usercontrols for Umbraco and
Yesterday I've posted question about SQLite performance issues ( Terrible performance in Zend Db
yesterday I took a database exam and the question about normalization was strange. We

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.