Encode and decode a wide range of C objects

This module may be used to encode and decode C objects such as integers, floats, doubles, times, and internationalized strings to and from a wide variety of binary formats as they might appear in portable file formats or network messages. These encodings include 16, 34, and 64 bit big and little endian intergers, big and little endian IEEE754 float and double values, 6 time encodings, and the wide range of string encodings supported by libiconv. The functions are all designed to be ideal for in-situ decoding and encoding of complex formats. The code is licensed under the GNU Library General Public License.

Why not just use XDR?

XDR is a specific format. The encdec functions are ideal for picking apart and building arbitrary encodings like binary file formats and network messages. Like XDR, it can be used very effectively to implement custom RPCs but that is incedental. XDR is also not available in certain environments like Java ironically. In this case encdec's open ended encoding/decoding capabilities could be used with whatever the peer encoder/decoder has available (e.g. DataInputStream and DataOutputStream).

How do encdec's string functions differ from iconv?

The encdec mbs functions are just a different interface into Bruno Haible's libiconv. They are more convenient for in-situ string manipulation.

For example, if you are given a string that you need to convert to the locale encoding (e.g. UTF-8) but you do not know how much memory the resulting string will occupy, something like the following code example could be used. It passes a NULL destination buffer to indicate to the dec_mbsncpy function that the exact number of bytes required to encode the resulting string should be returned. This value is used to allocate the correct amount of memory and the string is converted again with the dec_mbsncpy function but this time with the allocate memory instead of NULL:

  if ((n = dec_mbsncpy(&src, sn, NULL, -1, -1, "KOI8-R")) == (size_t)-1) {
      perror("Failed to determine required buf size");
      return -1;
  buf = malloc(n);
  if (!buf || (n = dec_mbsncpy(&src, sn, buf, n, -1, "KIO8-R")) == (size_t)-1) {
      perror("Failed to convert string");
      return -1;
  // buf is now the decoded string


API Reference
Browse The Source


encdec-0.4.0 released
Tue Jul 22, 2003
An Encdec Java class has been added to the package. The static methods in this class mirrot the C API. The encodings generated by both implementations are identical (except for a loss in precision with time encodings because C's time_t has a 1 second resolution vs. Java's millisecond resolution). This package should be ideal for reading and writing binary data between Java and C processes possibly over a network.

encdec-0.3.7 released
Sat Mar 21, 2003
The prototypes for float and double functions did not specify unsigned char src and dst parameters. This is technically incorrect and has been fixed. The documentation has also been enhanced to use the latest CStyleX package (Tip: in Mozilla you can use Ctrl+ and Ctrl- to increase and decrease the text size).

encdec-0.3.6 released
Sat Nov 16, 2002
IEEE754 functions have been added, a popular Windows time encoding has been added, and portability has been greatly improved (pretty much anywhere including Win32).

CStyleX Documentation
Sat Nov 16, 2002
An API reference, a man page, and this project page have been generated using the CStyleX package.

encdec-0.2.3 released
Fri Mar 22, 2002
The FLD macro for decoding bit fields has been reduced to a trivial and rather interesting expression.

encdec-0.2.2 released
Thu Mar 14, 2002
The dec_mbsncpy_new and dec_mbscpy_new function names have been changed to dec_mbsndup and dec_mbsdup to be more consistent with common C practice.

July 22, 2003