1 message
Open this post in threaded view
|

-------- Forwarded Message --------
Subject: Re: [erlang-questions] binary_to_integer badarg Mon, 17 Dec 2018 20:47:37 -0600 Dan Sommers [hidden email] Donald Steven [hidden email]

On 12/17/18 8:35 PM, Donald Steven wrote:> Dan,
```On 12/17/2018 9.15 PM, Dan Sommers wrote:
On 12/17/18 7:04 PM, Donald Steven wrote:

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.

This does what you want, but there may be
better ways (I'm an Erlang newbie, too):

<<Integer>> = Binary, Integer.

I'm sorry I don't understand.  Let's say I have a value of <<"%">>,
and assign it to B.  Let's say:

B = <<"%">>,
```

```If I want to assign the value of "%" (decimal 37) to integer I, how do
I complete the expression:

I =

such that I will equal 37.
```

Strictly speaking, I don't know. :-)

That said, Erlang's "=" operator isn't as assignment operator, it's a
pattern matching operator that binds its operands in the case of a
match. So the following function reads one character from the file
named by the given name, and returns the integer value of that
character:

f(FName) ->
{ok, Fd} = file:open(FName, [read, raw, binary]),
<<I>> = B,
I.

I think if you understand how/why the first line binds Fd to the file
descriptor, and how the second line binds B to the binary read from the
file , then you'll also be able to make sense of the third line that
binds I to the integer value of B.

Please direct further inquiries to the list rather than to me directly,
and feel free to forward this entire reply to the list for further
discussion.

Dan

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions