[erlang-questions] C data encoding/decoding

Bob Cowdery <>
Tue Sep 12 14:37:38 CEST 2006

I had another look and it made a bit more sense. Would this be anywhere close to what I need to do. I haven't even tried to compile it as I'm on the wrong machine. I can probably get away without having to know the arity in start_update() but I know how many methods calls will be encoded so it seems easier.

int index = 0;
int *pindex = &index;
ei_x_buff *buf;

void start_update(int arity) {
	index = 0;
	ei_x_encode_tuple_header(buf, &pindex, arity);

ei_x_buf *finish_update() {
	return buf;

void populate_band_control(RAD_B bands[]) {

	ei_x_encode_tuple_header(buf, &pindex, 2);
	ei x_encode string(buf, &index, "populate_band_control");
	ei x_encode list header(buf, &index, BAND_NO);
	for(i=0; i<BAND_NO; i++) {
		ei_x_encode_tuple_header(buf, &pindex, 5);
		ei_x_encode_double(buf, &index, rad_dt.bands[i].home);
		ei_x_encode_string(buf, &index, rad_dt.bands[i].type);
		ei_x_encode_double(buf, &index, rad_dt.bands[i].low_edge);
		ei_x_encode_double(buf, &index, rad_dt.bands[i].high_edge);
		ei_x_encode_string(buf, &index, rad_dt.bands[i].label);

-----Original Message-----
[mailto:]On Behalf Of Bob Cowdery
Sent: 12 September 2006 09:29
Cc: Erlang-Questions (E-mail)
Subject: Re: [erlang-questions] C data encoding/decoding

-----Original Message-----
From:  [mailto:]
Sent: 11 September 2006 13:05
To: Bob Cowdery
Subject: Re: [erlang-questions] C data encoding/decoding

Hi Bob!
Could you put your code somewhere we can help you?

Well, I've looked at ei and I still can't really figure how to use it. Maybe I'm being dumb, it wouldn't be the first time. This may seem an odd way to proceed but what I have done is do what I want in JSON (http://www.json.org/) and put the string in a term. The code snippet does at least explain what I want to achieve and was very quick to write. Note, this is not necessarily complete code, its just compiled, not tested and is the first JSON I have written.

The method populate_band_control() encodes one call to a remote proc. It adds this to the array of calls that are being encoded as a single message. The message is constructed by calling start_update(), then each call to be encoded and then finish_update() which returns an ETERM that says this is  JSON encoded and carries the JSON string. What is being encoded in this case is an array of the following structs. This is by no means the most complex of the encodings.

Would it be possible to do this in ei I guess is the question. Do you think what I am doing is a reasonable way to do things when C talks to C. My fear is that I am digging myself a hole should I now want one end of this to be pure Erlang as I would have to use the Erlang JSON module.


The type RAD_B defines one radio band
typedef struct radio_band
 double home;    // The default home frequency for this band
 char type[10];   // The type
 double low_edge;   // Lower edge of band
 double high_edge;   // Upper edge of band
 char label[10];   // Label displayed to the user

void start_update() {
update = json_object_new_array();
void add_to_update(struct json_object* item) {
json_object_array_add(update, item);
ETERM *finish_update() {
return erl_format("{JSON, ~s}", json_object_to_json_string(update));
void populate_band_control(RAD_B bands[]) {
// Construct the JSON message
struct json_object *band_dt, *bands_dt, *jsonmsg;
int i;
bands = json_object_new_array();
for(i=0; i<BAND_NO; i++) {
band_dt = json_object_new_object();
json_object_object_add(band_dt, "home", json_object_new_double(rad_dt.bands[i].home));
json_object_object_add(band_dt, "type", json_object_new_string(rad_dt.bands[i].type));
json_object_object_add(band_dt, "low", json_object_new_double(rad_dt.bands[i].low_edge));
json_object_object_add(band_dt, "high", json_object_new_double(rad_dt.bands[i].high_edge));
json_object_object_add(band_dt, "label", json_object_new_string(rad_dt.bands[i].label));
json_object_array_add(bands_dt, band_dt);
jsonmsg = json_object_new_object();
json_object_object_add(jsonmsg, "populate_band_control", bands_dt);

erlang-questions mailing list


More information about the erlang-questions mailing list