1
我有一个从erlang进程接收数据的C++代码。我得到一个元组,并使用ei_decode_tuple_header/4,我拿到名单的元数,然后用一个for循环遍历数组的元素遍历和解码这样他们每个人:如何使用ei_decode_term进行解码。
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;
}
}
然而,当元组/列表中的所有元素都是相同类型的时候,这种方法才有效。无论术语是什么,我都想解码。我知道有ei_decode_term,但是文档非常糟糕,我不知道如何去做。
有人可以帮忙!感谢@
感谢@ rorra,term.value指的是一个可以存放float,char,atom,pid,port的联合。如果万一,我给出一个形式元组{{a,b,c},[“d”,“e”,“f”],{“g”,“h”}}似乎并不是使用ei_decode_ei_term的方法。我想要一个通用的解决方案来解码任何erlang术语,并递归,如果这实际上是任何其他复合术语。任何指针?感谢您的详细解答 – root