Hello everybody,
I have a simple question :
Data = <<250:8>>,
....
now how can I retrieve 250 as an integer from Data ?
Hello everybody,
I have a simple question :
Data = <<250:8>>,
....
now how can I retrieve 250 as an integer from Data ?
Simply, <<Data>> = <<250:8>>
.
Imagine <<
and >>
for binaries just as {
and }
for tuples and it will come naturally to you.
Thank you @mmin for your answer, but Data as you bind is the binary representation of 250 and not an integer 250
Hi @Abdelghani,
The Data in the example of @mmin is actually an integer, with the value 250.
You can check it in the Erlang shell:
Erlang/OTP 24 [erts-12.0.4] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit]
Eshell V12.0.4 (abort with ^G)
1> <<N>> = <<250:8>>.
<<"Ăş">>
2> N.
250
3> is_integer(N).
true
More info related to your question you can get from the presentations Bit-level Binaries and Generalized Comprehensions in Erlang or from this article Bit-level Binaries and Generalized Comprehensions in Erlang *.
Another way to solve the proposed task:
BinaryData = <<250:8>>.
<<Value:8>> = BinaryData.
<<Value:8/integer-unit:1>> = BinaryData.
You can check it in the Erlang shell:
Eshell V12.2 (abort with ^G)
1> BinaryData = <<250:8>>.
<<"·">>
2> <<Value:8>> = BinaryData.
<<"·">>
3> Value.
250
4> is_integer(Value).
true
5> BanaryData = <<Value>>.
<<"·">>
6> <<Value/binary>> = BinaryData.
** exception error: no match of right hand side value <<"·">>
7> <<Value:8/binary>> = BinaryData.
** exception error: no match of right hand side value <<"·">>
8> <<Value:8/binary-unit:8>> = BinaryData.
** exception error: no match of right hand side value <<"·">>
9> <<Value:8/integer-unit:1>> = BinaryData.
<<"·">>
10>
thank you @mmin and @Anatolii for your answers, the trick is that I used <<Value:8/bits>> = Data
because I thinked that bits
is the default specified type in Erlang and when I have see your answers I readed again the manual and found that integer
is the default.
so when we write
<<Value:8>> = Data
it’s equivalent to
<<Value:8/integer>> = Data
if we do this
<<Value:8/bits>> = Data
then Value will bound to <<"Ăą">>
Iam very thankful to be with you here.
@Anatolii thank you for the links, since the erlang bit syntax documentation is rare and all that I can found is the same manual documentation, so this will be useful.
@Abdelghani, I glad you have found an answer and understand more deeper that part of Erlang programming language.
Joe Armstrong’s book “Programming Erlang” has valuable chapter related to this topic. It is Chapter 7. “Binaries and the Bit Syntax”. I recommend that you familiarize yourself with its contents for even more detailed acquaintance with this issue.
Francesco Cesarini and Simon Thompson’s book “Erlang Programming” also has information ralated this topic. It is Chapter 9. “More Data Types and High-Level Constructs”. Subpart “Binaries and Serialization”.
Fred Hébert’s book “Learn You Some Erlang for great good!” In a part “Bit syntax” you get information too related to bit syntax tips and tricks too.
Thanks again @Anatolii for your help, I have already read all books that you have recommend but any of them have explained the bit syntax and especially matching rules much enough than the abstract in your previous answer