将erlang项转换为字符串,或解码erlang二进制 [英] Convert erlang terms to string, or decode erlang binary

查看:311
本文介绍了将erlang项转换为字符串,或解码erlang二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成数据的erlang程序。该数据需要通过udp传输到非erlang程序进行进一步处理。我已经有这部分工作 - 通过udp发送数据,并在另一个非erlang端接收数据。



这是问题。数据(像包含列表的元组这样的erlang术语)看起来似乎不能按原样(即我不能只是发送任意的erlang术语)。它显然需要首先转换为文本或二进制文件。转换为二进制似乎很容易,我发现一个bif。问题是,二进制gobbledygook出来的另一边,我不知道任何简单的方法来解码(另一方面是非erlang)。



限制有人告诉我一些简单的方法来解码另一边的二进制gobbledygook,我想要将数据作为一个简单的字符串表示形式发送,例如这样的元组:

  {[1,2,3],[4,5,6]} 

发送如下:

 {[1,2,3],[4 ,5,6]}

我还没有看到任何这样的bif,即convert_term_to_ascii / 1 等等。我知道我可以扫描它并发送令牌表示的术语,但我不想这样做 - 在另一方面的解码只是一个痛苦,我不想处理。



我知道我不是第一个,第二个或第三个人有这个问题。它必须相当普遍。如何正确处理?



有人可以指出一些资源,告诉我如何将二进制gobbledygook转换为ascii(非erlang方面需要) ,或2)直接将术语转换成字符串(需要在erlang方面)?



或者,告诉我我怎么错了,我该怎么做这个



谢谢。

解决方案

1)使用

  R = io_lib:format(〜p,[yourtermhere]),
列表:flatten (R)

2)你可能会看看erlang外部二进制格式,很多其他语言都有库用于对erlang二进制格式进行编码/解码。而在erlang中,您可以使用term_to_binary


对任何术语进行编码

I have an erlang program which generates data. This data needs to be transferred via udp to a non-erlang program for further processing. I already have this part working - sending the data via udp and receiving it on the other non-erlang side.

Here's the problem. The data (erlang terms like tuples containing lists) doesn't seem to be able to go over "as is" (i.e. I can't just send arbitrary erlang terms). It apparently needs to be converted to either text or binary first. Converting to binary seems easy enough with a bif I found. The problem is, binary gobbledygook comes out the other side, and I don't know any easy way to decode it (the other side is non-erlang).

Barring someone telling me some easy way to decode binary gobbledygook on the other side, I'd like the data to be sent as a simplistic string representation of the terms - for instance a tuple like this:

{[1,2,3],[4,5,6]}

sent like this:

"{[1,2,3],[4,5,6]}"

I haven't seen any such bif, i.e. "convert_term_to_ascii/1" etc. I know I could scan it and send token representations of the terms, but I don't want to do that - decoding that on the other side is just a pain I don't want to deal with.

I know I'm not the first, second, or third person to have this problem. It has to be fairly common. How is it normally dealt with?

Can someone point me to some resource showing me how to either 1) convert binary gobbledygook to ascii (needed on the non-erlang side), or 2) straightforwardly convert terms to a string (needed on the erlang side)?

Or, tell me how I'm wrong and how I should really be doing this?

Thanks.

解决方案

1) you can convert any term to string using

R= io_lib:format("~p",[yourtermhere]),
lists:flatten(R)

2) you might look at erlang external binary format, a lot of other languages have libraries for encode/decode that erlang binaries format. And in erlang you can encode any term by term_to_binary

这篇关于将erlang项转换为字符串,或解码erlang二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆