read numbers

Raimo Niskanen raimo+erlang-questions@REDACTED
Thu Dec 9 15:50:12 CET 2021


On Thu, Dec 09, 2021 at 02:41:40PM +0100, Java House wrote:
> Ok I think I found the solution, which is exactly what I wanted to read
> each digit as a number
> 26> <<P1:8, P2:8, P3:8, P4:8, P5:8, P6:8, P7:8, P8:8, P9:8, P10:8, P11:8,
> P12:8>> = <<"111011001010">>.
> <<"111011001010">>
> 27> list_to_integer([P1]).
> 1

Using list_to_integer for that is really overkill.

These have been suggested before:

    case P1 of
	$0 -> 0;
	$1 -> 1
    end

And

    P1 - $0

With range check:

    if
	$0 =< P1, P1 =< $1 ->
	    P1 - $0
    end

How do you want to handle <<"101010101013">>,
<<"  101010101010">>, <<"10101010101  ">>,
<<"10101010101q">>, <<"101010 101010">>
and other strange input?

What are you going to do next with P1 .. P12?

E.g, assuming you want each line to be parsed as one integer:

    bstr2uint(Bin) -> bstr2uint(Bin, 0).
    %%
    bstr2uint(<<P, Rest/binary>>, Int) ->
	case P of
	    $0 -> bstr2uint(Rest, Int bsl 1);
	    $1 -> bstr2uint(Rest, (Int bsl 1) bor 1)
	end;
    bstr2uint(<<>>, Int) -> Int.


> 
> 
> Στις Πέμ 9 Δεκ 2021 στις 2:29 μ.μ., ο/η Java House <java4dev@REDACTED>
> έγραψε:
> 
> > 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.
> >
> > I also tried to do something like that to convert binaries to digits
> >
> > binary_to_digit(<<"0">>) -> 0;
> > binary_to_digit(<<"1">>) -> 1.

That converts a binary of one byte into an integer.
I guess you want to convert a binary of 12 or N bytes.

See the recursion loop in bst2uint/2 above.

> >
> > but the problem is that once you match a binary
> >
> > parse([<<P1:8,P2:8....P12:8>>|T])
> >
> > The P1 parameter is not any more binary it is integer.
> >
> > I did a small example to show problem,
> > you have in the file  111011001010
> > You read it as binary which is something similar to
> > 12> <<P1:8, P2:8, P3:8, P4:8, P5:8, P6:8, P7:8, P8:8, P9:8, P10:8, P11:8,
> > P12:8>> = <<"111011001010">>.
> > <<"111011001010">>
> > 13> P1.
> > *49*

49 is the ASCII value for the character "1".
That value has got the Erlang syntax `$1'.

    $1 = 49
    "1" = [$1] = [49]
    "10" = [$1, $0] = [49, 48]
    "ABC" = [$A, $B, $C] = [65, 66, 67]

"10" is syntactical sugar for a list of integers with the ASCII
values for the characters `1' then `0'.

<<"10">> is syntactical sugar for a binary with the integers
of the ASCII values for the characters `1' then `0'.

    <<"10">> = <<$1, $0>> = <<49, 48>>
    <<"ABC">> = <<$A, $B, $C>> = <<65, 66, 67>>


> > 15> integer_to_binary(P1).
> > <<"49">>
> >
> > So it seems that even though the digit is 1 Erlangs parsing/matching
> > converts it to something else and it seems impossible to read exactly what
> > was the original value, which is a simple 0 or 1.
> >
> >
> > Στις Πέμ 9 Δεκ 2021 στις 12:09 μ.μ., ο/η Roger Lipscombe <
> > roger@REDACTED> έγραψε:
> >
> >> 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 <java4dev@REDACTED> 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
> >> >
> >> >
> >>
> >

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list