read numbers

Richard O'Keefe raoknz@REDACTED
Fri Dec 10 06:02:12 CET 2021


Question 1:
  Why is the file written that way in the first place?
  With multi-terabyte drives as cheap as they are, you
  may not care about bloating the space your data use,
  but more characters => more time.

Question 2:
  Depending on what bit manipulation you want to do,
  it may be more convenient to work with integers
  rather than binaries.  So what exactly do you want
  to do with these?

b2i(Binary) ->
    b2i_loop(Binary, 0).

b2i_loop(<<Digit:8,Rest/binary>>, N)
  when $0 =< Digit, Digit =< $1 ->
    b2i_loop(Rest, N*2 + (Digit - $0));
b2i_loop(<<>>, N) ->
    N.

I have tested this code for converting <<"...">>
from a sequence of 0/1 characters to an integer.


On Fri, 10 Dec 2021 at 08:10, Java House <java4dev@REDACTED> wrote:

> Hi Kenneth
> my problem is that I need to extract every bit separately.
> So I need to first bit of each number and the second and the third etc.
> I want to do bit manipulation not just read the number.
>
> But you answered my second question on how to convert a list of digits to
> a binary.
> So this is the second part of my problem
>
> 29> lists:concat([1,1,0,0,1,1,0,0,1,0,1,0]).
> "110011001010"
> 30> list_to_binary(lists:concat([1,1,0,0,1,1,0,0,1,0,1,0])).
> <<"110011001010">>
> 31>
> binary_to_integer(list_to_binary(lists:concat([1,1,0,0,1,1,0,0,1,0,1,0]))).
> 110011001010
> 32>
> binary_to_integer(list_to_binary(lists:concat([1,1,0,0,1,1,0,0,1,0,1,0])),
> 2).
> 3274
>
> Too many conversations for just a simple bit manipulation.
>
> Thank you all for your contribution.
>
> Kind Regards
> Nikolas
>
>
> Στις Πέμ 9 Δεκ 2021 στις 7:33 μ.μ., ο/η Kenneth Lundin <kenneth@REDACTED>
> έγραψε:
>
>> 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 <java4dev@REDACTED> 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 <
>>> roger@REDACTED> έγραψε:
>>>
>>>> On Thu, 9 Dec 2021 at 13:29, Java House <java4dev@REDACTED> 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]]
>>>>
>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20211210/1dd42347/attachment-0001.htm>


More information about the erlang-questions mailing list