binary_to_integer badarg

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

binary_to_integer badarg

Donald Steven
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.

Your help would be greatly appreciated.  Thanks.

Don
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: binary_to_integer badarg

Dan Sommers
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.

Dan
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: binary_to_integer badarg

Lukas Larsson-8
In reply to this post by Donald Steven


On Tue, Dec 18, 2018 at 2:04 AM Donald Steven <[hidden email]> 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

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: binary_to_integer badarg

Donald Steven

Thanks Lucas and Dan.  This solved my problem.

On 12/18/2018 2:39 AM, Lukas Larsson wrote:


On Tue, Dec 18, 2018 at 2:04 AM Donald Steven <[hidden email]> 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

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