Click here to Skip to main content
13,446,805 members (41,207 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 29 Mar 2013

Encode and Decode messages send to and from apps

, 29 Mar 2013
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) serupIT
Denmark Denmark
I have been working with languages ranging from assembly 68000 to COBOL, OOC, MySQL, Perl, Java 8, JavaScript, C#, C++17, VC++ MFC and WinSockets.

I am currently working as a freelance software developer

You may also be interested in...

Comments and Discussions

QuestionTwo years later... Pin
OriginalGriff29-Mar-13 2:41
mvpOriginalGriff29-Mar-13 2:41 
GeneralNot an article yet... [modified] Pin
Dave Kreskowiak14-May-11 3:10
mvpDave Kreskowiak14-May-11 3:10 
JokeRe: Not an article yet... Pin
Richard MacCutchan14-May-11 22:15
mvpRichard MacCutchan14-May-11 22:15 
GeneralRe: Not an article yet... Pin
Dave Kreskowiak15-May-11 2:30
mvpDave Kreskowiak15-May-11 2:30 
GeneralRe: Not an article yet... Pin
serup9-Jun-11 20:41
memberserup9-Jun-11 20:41 
GeneralRe: Not an article yet... Pin
Dave Kreskowiak10-Jun-11 1:32
mvpDave Kreskowiak10-Jun-11 1:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03-2016 | 2.8.180318.2 | Last Updated 29 Mar 2013
Article Copyright 2013 by serup
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid