Click here to Skip to main content
12,298,379 members (52,705 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked

Encode and Decode messages send to and from apps

, 29 Mar 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Makes message handling between applications easier, by encoding/decoding to/from one data block.


The code is a Data Encoder / Decoder library. It makes inter application communication easier, by providing a way to encode and decode a message based on a user defined protocol.

It also makes a simple compression of the "message" created by the encoder part. The decoder part will then decompress and decode message, based on the provided protocol


It is using a simplified version of ASN (Abstract Syntax Notation) to encode / decode the data, and the compression algorithm is based on a simplified version of Hoffman coding

Using the code

The DataEncoderDecoder is a standard static library and should be added to your project as a header file and a library file - the library is static which means that it will be embedded in your projects executable

Example: Creating an event message to be send to a server asking it to use method MusicPlayer passing on a transaction id and action Start/Stop.  All data is in pCompressedData!!! It is this memory that should be send:

// Client code
// Create DataEncoderDecoder response
// Encode
DED_PUT_STRUCT_START( encoder_ptr, "event" );
    DED_PUT_METHOD    ( encoder_ptr, "name",      "MusicPlayer" );
    DED_PUT_USHORT    ( encoder_ptr, "trans_id",  trans_id);
    DED_PUT_BOOL    ( encoder_ptr, "startstop", action );
DED_PUT_STRUCT_END( encoder_ptr, "event" );

// Data to be sent is in pCompressedData

// Server code
// retrieve data ...

CString strName,strValue;
unsigned short iValue;
bool bValue;


// decode data ...
if( DED_GET_STRUCT_START( decoder_ptr, "event" ) &&
     DED_GET_METHOD    ( decoder_ptr, "name", strValue ) &&
     DED_GET_USHORT    ( decoder_ptr, "trans_id", iValue) &&
     DED_GET_BOOL    ( decoder_ptr, "startstop", bValue ) &&
 DED_GET_STRUCT_END( decoder_ptr, "event" ))

Points of Interest

When trying to use this type of data encoder / decoder, then remember from which type of computer you are sending / receiving, I am thinking about the Big/Little  Endian principle - so far the DataEncoderDecoder does not take care of this (perhaps a smart reader could add this)  Smile | :)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) Nokia
Denmark Denmark
I have been working with languages ranging from assembly 68000 to COBOL, OOC, MySQL, Perl, VC++ MFC and WinSockets.

I am currently working as a senior software architect at Nokia Mobile Phones Denmark

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160525.2 | Last Updated 29 Mar 2013
Article Copyright 2013 by serup
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid