# Extract an integer that was been converted to binary

Hello everybody,
I have a simple question :

``````Data = <<250:8>>,
....
``````

now how can I retrieve 250 as an integer from Data ?

4 Likes

Simply, `<<Data>> = <<250:8>>`.
Imagine `<<` and `>>` for binaries just as `{` and `}` for tuples and it will come naturally to you.

3 Likes

Thank you @mmin for your answer, but Data as you bind is the binary representation of 250 and not an integer 250

2 Likes

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
``````
6 Likes

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>
``````
3 Likes

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.

4 Likes

@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.

3 Likes

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

2 Likes