Re: read numbers

Roger Lipscombe <[email protected]>
Newsgroups gmane.comp.lang.erlang.general
Message-ID <CAJgnQd8x0vvU5Rhwg7=pSZyA1gA5W8ZC0K4obnf4aWj4SavBJQ@mail.gmail.com>
You could pattern match on the "0" and "1" directly...

parse(Data) -> parse(Data, []).
parse(<<"0", Rest/binary>>, Acc) ->
    [false | parse(Rest, Acc)];
parse(<<"1", Rest/binary>>, Acc) ->
    [true | parse(Rest, Acc)];
parse(<<$\n, Rest/binary>>, Acc) ->
    parse(Rest, Acc);
parse(<<>>, Acc) -> Acc.

On Thu, 9 Dec 2021 at 09:15, Java House <[email protected]> wrote:
>
> Hello
>
> I am trying to read a file like with a series of 0s and 1s e.g.
>
> 111011001010
> 010011101110
> 110001001010
> 001101011101
> 110100000011
> 010110110010
>
>
> I read the file with
>
> {ok, Data} = file:read_file("binary.input"),
>
> which gives me a list of binaries and then by binary matching I get the individual numbers in binary format
>
> parse([<<P1:8,P2:8....>>|T])
>
> But now I am having the following problem
>
> each P? may contain 0 or 1 but in reality it contains the ascii value of the character 0 or 1
>
> that is 48 or 49.
>
> How do a get from the ascii value the actual number 0 or 1?
>
>
> Kind Regards
>
> Nikolas
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.