1
            A= term_to_binary({100,200,123,45}) ,
            io:format("~p~n",[A]),
            mongo:save(?IP_ADDRESS, {'_id', 1, ip,A}).

the result is <<131,104,4,97,100,97,200,97,123,97,45>>

and I need to read it in java

    DBObject a = IpTable.findOne(new BasicDBObject("_id",1));
    byte[] t=null;
    try
    {
        String u = a.get("ip").toString();
        System.out.println(u);
            t= u.getBytes("ISO8859-1");
    }
    catch(Exception ex)
    {

    }
    for(int i=0;i<t.length;i++)
    System.out.print(t[i]+",");
    System.out.println();

the result is

63,104,4,97,100,98,0,0,7,63,97,123,97,45,

if I change the first bytes into 131

    t[0]=(byte)131;
    try
    {
        OtpInputStream ois = null;
        ois = new OtpInputStream(t);
        OtpErlangTuple IP = (OtpErlangTuple)OtpErlangObject.decode(ois);
        System.out.println(IP);}

the result is {100,63,123,45} is wrong

ter
  • 289
  • 1
  • 3
  • 6
  • its probably an encoding issue when you `toString()` the object or `getBytes()` the data. Check out http://stackoverflow.com/questions/16909645/how-to-read-erlang-term-from-redis-by-using-java-client/19369461#19369461 – Daniel Dec 20 '13 at 16:15

1 Answers1

0

By specifying term_to_binary, you are trying to convert erlang term to binary. The same should be converted using binary_to_term to get back the result. Storing thing information in mongodb and you are trying to do a different conversion here.

Hence you result is wrong

1> A= term_to_binary({100,200,123,45}).
<<131,104,4,97,100,97,200,97,123,97,45>>
2> binary_to_term(A).
{100,200,123,45}

and

String u = a.get("ip").toString();

in short "{a,b,c,d}" in java is not same as term_to_binary({a,b,c,d}) of erlang

here you are converting the binary erlang term to string. if you want to save you have to save to string and then binary and then retrieve it.

Marutha
  • 1,814
  • 1
  • 12
  • 10