首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用ei_decode_term进行解码。

如何使用ei_decode_term进行解码。
EN

Stack Overflow用户
提问于 2016-04-21 07:57:02
回答 1查看 143关注 0票数 0

我有一个从erlang进程接收数据的c++代码。我得到了一个元组,并使用ei_ decode _ tuple _header/4,我获得了列表的数量,然后使用for循环遍历元组的元素,并像这样解码每个元素:

代码语言:javascript
复制
void decode_tuple(char *buff) {
   int index = 0;
   int size;
   int type;
   int res = ei_decode_tuple_header(buff, &index, &size);
   if(res == 0) {
     cout<<"Success"<<endl;
   } else {
     cout<<"Fail"<<endl;
   }

  for(int i = 0; i < size; ++i) {
    char *p = (char*)malloc(sizeof(char) * 1000);
    int res = ei_decode_string(buff, &index, p);
    if(res == 0) {
        cout<<"Success"<<endl;
    } else {
        cout<<"Fail"<<endl;
    }
    cout<<"The decoded string is "<<p<<endl;
    }
  }

然而,只有当tuple/list中的所有元素都属于同一类型时,这种方法才能正常工作。不管是什么术语,我都想解码。我知道有ei_decode_term,但是文档太糟糕了,我不知道该怎么做?

谁来帮帮忙!谢谢@

EN

回答 1

Stack Overflow用户

发布于 2016-04-21 12:41:53

你有没有尝试过使用ei_decode_ei_term,比如:

代码语言:javascript
复制
void decode_tuple(char *buff) {
  int index = 0;
  int size;
  int type;
  int res = ei_decode_tuple_header(buff, &index, &size);

  if(res == 0) {
    cout << "Success" << endl;
  } else {
    cout << "Fail" << endl;
  }

  for(int i = 0; i < size; ++i) {
    ei_term term;
    int res = ei_decode_ei_term(buff, &index, &term);
    if (res == 0) {
      cout << "Success" << endl;
    } else {
      cout << "Fail" << endl;
    }
    cout << "The decoded data is " << term.value << endl;
  }
}

ei_term的定义如下:

代码语言:javascript
复制
typedef struct {
    char ei_type;
    int arity;
    int size;
    union {
        long i_val;
        double d_val;
        char atom_name[MAXATOMLEN_UTF8];
        erlang_pid pid;
        erlang_port port;
        erlang_ref ref;
    } value;
} ei_term;

因此,您可能需要检查term.ei_type以获得更好的解析

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36757254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档