Re: read numbers
Kenneth Lundin <[email protected]>
| Newsgroups | gmane.comp.lang.erlang.general |
|---|---|
| Message-ID | <CAOzgw92-Qm-8+ZTGTTWpFaNM-B2ckToxz+k8cUhuJ9APVtP8EQ@mail.gmail.com> |
Why not read it with
ok, Data} = file:read_file("binary.input"),
then split the binary to a list of lines
BinList = binary:split(Data,<<"\n">>,[global])
each element in the list is a binary like this:
B = <<"1010101100">>
covert this to an integer with:
binary_to_integer(B,2)
and there you have your integer which you can do whatever you like with
Note, this is written on my phone so it is not tested, could be some
fauly detail
/Kenneth
On Thu, Dec 9, 2021 at 6:52 PM Java House <[email protected]> wrote:
> Hi Roger thank you for replying.
> I am having a series of 0s an1s in a List and want to convert it to a
> decimal number.
> I am looking for something similar to BItSet in java
>
> Kind Regards
> Nikolas
>
> Στις Πέμ 9 Δεκ 2021 στις 3:24 μ.μ., ο/η Roger Lipscombe <
> [email protected]> έγραψε:
>
>> On Thu, 9 Dec 2021 at 13:29, Java House <[email protected]> wrote:
>> > Thank you Roger for the answer
>> > I thought about it but since I have to parse all digits for every row
>> that would mean a lot of entries as I have to create all possible
>> combinations of 0/1 for every position of the row.
>>
>> parse(Data) -> parse(Data, [], []).
>>
>> parse(<<"0", Rest/binary>>, Line, Lines) ->
>> parse(Rest, [false | Line], Lines);
>> parse(<<"1", Rest/binary>>, Line, Lines) ->
>> parse(Rest, [true | Line], Lines);
>> parse(<<$\n, Rest/binary>>, Line, Lines) ->
>> parse(Rest, [], [lists:reverse(Line) | Lines]);
>> parse(<<>>, [], Lines) -> lists:reverse(Lines);
>> parse(<<>>, Line, Lines) -> lists:reverse([lists:reverse(Line) | Lines]).
>>
>> ...results in...
>>
>> [[true,true,true,false,true,true,false,false,true,false,true,false],
>> [false,true,false,false,true,true,true,false,true,true,true,false],
>> [true,true,false,false,false,true,false,false,true,false,true,false],
>> [false,false,true,true,false,true,false,true,true,true,false,true],
>> [true,true,false,true,false,false,false,false,false,false,true,true],
>> [false,true,false,true,true,false,true,true,false,false,true,false]]
>>
>