[erlang-questions] Simle Erlang Question (Binary "String" to Erlang "String")

Vance Shipley vances@REDACTED
Sun Sep 28 05:02:30 CEST 2008


On Sun, Sep 28, 2008 at 10:03:35AM +0930, David Lloyd wrote:
}  I have gotten this:
}  
}    3> {ok, File}=file:read_file("hello.b64").
}    {ok,<<"SGVsbG8sIHdvcmxkIQ==\n">>}
}  
}  I want to make File into an Erlang string so that effectively if I asked 
}  for its value in the shell I'd get something like this:
}  
}    4> File.
}    "SGVsbG8sIHdvcmxkIQ==\n"

You can use the built in function (BIF) binary_to_list/1.

     5> binary_to_list(File).
     "SGVsbG8sIHdvcmxkIQ==\n"

If you just want to read Erlang terms from a file of your design
you can use file:consult/1.  To do this you write the term(s) in
erlang format:

     "SGVsbG8sIHdvcmxkIQ==\n".
     "Another string".
     an_atom.
     {tuple, a}.

And read them with:

     6> file:consult("foo.txt").
     {ok,["SGVsbG8sIHdvcmxkIQ==\n","Anorther string",an_atom, {tuple,a}]}


  -Vance




More information about the erlang-questions mailing list