简体   繁体   中英

Encode data with ASN.1

I tried to follow this example to encore data and send them to the server.

CertainStructure_t *cs = 0;
        ber_decode(0, &asn_DEF_CertainStructure, &cs, buffer, buffer_length);
        cs->val1 = 123;        /* Modify the contents */
        der_encode(&asn_DEF_CertainStructure, cs, write_handle, 0);

I wrote this code

Message01_t *a;
a->number = 123;
der_encode(&Message01, a, write_handle, 0);

the description of Message01 is

Message01 ::= SEQUENCE
        {
          number INTEGER  -- inital integer
        }

I got this error

error: incompatible types in assignment
error: ‘Message01’ undeclared (first use in this function)

On the assumption this is the online ASN.1 Compiler at http://lionet.info/asn1c/asn1c.cgi

And just trying to get it compiling.... Not really knowing what I'm doing:

The following just compiled:

#include <Message01.h> 

int write_handle(void const*, unsigned long, void*) {
    // no idea what this is
    return 0;
}

int main() {        
    Message01_t *a=new Message01_t(); // Must be created/allocated/whatever
    a->number = 123;
    der_encode(&asn_DEF_Message01, a, write_handle, 0);
}

Notes:

  1. As your example indicated you must use &asn_DEF_MyTypeName. Seems to be a generated static variable with a descriptor of the ASN.1 type.
  2. Didn't get the assignment error. number is a long... so that should be fine.
  3. Must allocate something for the Message01_t *a variable. I've used C++, pick yours... alternatively use a local variable and &a in the der_encode call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM