Shell代表二进制 [英] Shell represents binary

查看:169
本文介绍了Shell代表二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下声明

iex(5)> a = <<3>>
<<3>>
iex(6)> b = <<a::binary>>
<<3>>

第一行,我创建了一个值为3的二进制文件。第三行,我希望shell显示我00000011不是3。我知道先创建一个binary(1.line)然后再转换为binary没有任何意义。但是我期望shell显示00000011而不是3。

First line, I created a binary with value 3. On third line I want that the shell shows me 00000011 not 3. I know it does not make sense first the create a binary(1.line) then convert to binary again. But I was expecting that shell shows me 00000011 instead of 3.

当二进制浮点型时

iex(7)> a = << 5.3 :: float  >>
<<64, 21, 51, 51, 51, 51, 51, 51>>

我不明白,为什么它显示出这个数字?

I do not understand, why it shows me this numbers?

推荐答案

二进制是内存中的字节序列。打印的表示与此定义匹配。打印输出中的每个数字都在0到255之间(这些数字可以表示所有可能的字节值)。

Binary is a sequence of bytes in memory. The printed representation matches this definition. Every number in printed output is between 0 and 255 (those numbers can represent all possible values of byte).

二进制文件是解析和编码协议或其他协议的简便快捷方式通过网络发送到外部系统的消息。例如,这段代码解析ip协议:

Binaries are cool and easy way to parse and encode protocols or other messages to be send over the network to external systems. For example this piece of code parses ip protocol:

<< protocol_version :: size(4),
   size_in_words :: size(4),
   tos :: size(8),
   total_length :: size(16),
   identifier :: size(16),
   flags :: size(3),
   fragment_offs :: size(13),
   ttl :: size(8),
   protocol :: size(8),
   header_checksum :: size(16),
   src_ip :: size(32),
   dst_ip :: size(32),
   options :: size(32),
   data :: binary >> = bits

您可以在二进制文件的不同部分进行匹配。 size 以位为单位。最后一部分可以没有大小,但它的大小必须可被8整除(必须为n个完整字节)。

You can match on different parts of binary. size is specified in bits. Last part can be without size, BUT it needs to have size divisible by 8 (it has to be n full bytes).

较少的复杂示例可能是数字128,其中

Less complicated example might be number 128, which is 10000000 in binary.

iex(1)> bin = <<128>>
<<128>>
iex(2)> <<a::size(2), b::size(6)>> = bin
<<128>>
iex(3)> a # a matched bits 10, which is 2
2
iex(4)> b # b matched bits 000000 which is 0
0

相同的语法可用于从零件创建二进制文件。让我们重新使用变量a和b,但将它们放在不同的位置。

The same syntax can be used to create binaries from parts. Lets reuse variables a and b, but put them in different places.

iex(5)> anotherbin = <<a::size(5), b::size(3)>>
<<16>>

现在,第一部分为00010,第二部分为000,即00010000,即16。事情是您可以使用除浮点数之类的整数以外的其他东西来创建二进制文件,因此

Now the first part is 00010 and second part is 000, which gives 00010000, which is 16. Another thing is that you can create binaries from other things than integers like floats, so

iex(6)> a = << 5.3 :: float  >>
<<64, 21, 51, 51, 51, 51, 51, 51>>

创建另一个具有浮点值编码的二进制文件。现在,您可以通过网络发送它,并在另一端对其进行解码。因此,这与您的想法相反。

creates another binary that has the float value encoded. You can now send it over the network and decode it on the other side. So it is opposite to what you thought. It is not binary as a float, but float as a binary.

要真正看到东西的二进制表示,请尝试使用 to_string 来自不同模块的方法。它们具有可选的参数,是您要在其中打印输出的基础。

To actually see binary representation of stuff try using to_string methods from different modules. They have optional argument that is base in which you want to print the output.

iex(7)> Integer.to_string(128, 2)
"10000000"

还有一个陷阱。 Elixir中的字符串实际上是二进制文件。 Shell检查所有字节是否都是可打印字符。如果是,则将它们打印为字符串

There is one more catch. Strings in Elixir are actually binaries. The shell checks if all bytes are printable characters. If yes, then prints them as a string

iex(20)> <<100>>
"d"

您可以强制 IO.inspect 可以将二进制文件打印为字节或字符串列表:

You can force IO.inspect to print binaries as list of bytes or strings if you want:

iex(29)> IO.inspect <<100>>, [{:binaries, :as_binaries}]
<<100>> #this is what IO.inspect printed
"d"     #this is a return value, which is the same, but printed as string

如果强制将不可打印的字符解释为字符串,则将其转义:

If you force non printable characters to be interpreted as strings, they will be escaped:

iex(31)> IO.inspect <<2>>, [{:binaries, :as_strings}]
"\x02" #this is what IO.inspect printed
<<2>>  #return value printed by shell

这篇关于Shell代表二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆