Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert erlang terms to string, or decode erlang binary

Tags:

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.

like image 632
X Z Avatar asked Feb 24 '12 00:02

X Z


People also ask

Is binary in Erlang?

Use a data structure called a binary to store large quantities of raw data. Binaries store data in a much more space efficient manner than in lists or tuples, and the runtime system is optimized for the efficient input and output of binaries.

Is string an Erlang?

Strings are enclosed in double quotes ("), but is not a data type in Erlang.


2 Answers

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

like image 84
Odobenus Rosmarus Avatar answered Oct 08 '22 06:10

Odobenus Rosmarus


I'd recommend converting the erlang terms into JSON, with either of known libraries (heard good words regarding rfc4267). It'd be a trivial task to convert JSON back with any non-erlang platform, I guess. )

like image 22
raina77ow Avatar answered Oct 08 '22 06:10

raina77ow