[erlang-questions] binary_to_integer badarg

Lukas Larsson lukas@REDACTED
Tue Dec 18 08:39:40 CET 2018


On Tue, Dec 18, 2018 at 2:04 AM Donald Steven <t6sn7gt@REDACTED> wrote:

> Hi all,
>
> A newbie question (I've tried for hours to figure this out, but failed):
>
> I want to read a single character from a file, and have it return the
> integer representation of the character.  I've used:
>
> file:open(Fname, [read, raw, binary]) to open the file and
> file:read(Fd,1) to retrieve a character.
>
> This works fine and I'll get back, for example <<"%">>, which is_binary
> tells me is a binary.  I've tried binary_to_integer(<<"%">>) but I get a
> badarg error.
>

binary_to_integer converts the binary text representation of an integer to
an integer, i.e. binary_to_integer(<<"123">>) -> 123.

To get the integer value of a binary you can either use pattern matching as
Dan suggestest:


> <<Integer>> = Binary, Integer.
>

or you can use binary:decode_unsigned(<<"%">>) -> 37.

Lukas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20181218/bec8e2dd/attachment.htm>


More information about the erlang-questions mailing list