I convert a decimal number to binary format:
A = integer_to_list(5, 2). And I add to the list a list of zeros:
A ++ [0, 0, 0, 0, 0]. I get [49, 48, 49, 0, 0, 0, 0, 0] . It is necessary at the union to get [1, 0, 1, 0, 0, 0, 0, 0] .
I convert a decimal number to binary format:
A = integer_to_list(5, 2). And I add to the list a list of zeros:
A ++ [0, 0, 0, 0, 0]. I get [49, 48, 49, 0, 0, 0, 0, 0] . It is necessary at the union to get [1, 0, 1, 0, 0, 0, 0, 0] .
The fact is that you get a list of characters "101", not numbers. Characters have a numeric representation, according to the ASCII table, and code 0 corresponds to code 48, and character 1, respectively, 49.
Try to subtract the code of the digit 0 from each element of the array, obtained after calling the function integer_to_list(5, 2) .
This can be done, for example, as follows:
A = lists:map(fun(X) -> X-$0 end, integer_to_list(5, 2)). A bit more sane way:
i2bl(I) when is_integer(I) -> i2bl(<<I>>, []). i2bl(<<>>, Acc) -> Acc; i2bl(<<B:1, Rest/bitstring>>, Acc) -> i2bl(Rest, [B|Acc]). Example of use:
1> i2bl(5). [1,0,1,0,0,0,0,0] Source: https://ru.stackoverflow.com/questions/419769/
All Articles