欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Elixir中的String、binaries和bitstring

程序员文章站 2024-03-19 19:22:28
...

http://elixir-lang.github.io/getting-started/binaries-strings-and-char-lists.html

https://elixirforum.com/t/bitstring-and-binary/2351/4

字符串是怎么保存到计算机中的

iex>?a
97
iex>?吴
21556

a的code point是97,也就是110 0001;吴的code point是21556,也就是101 0100 0011 0100。这是unicode标准规定的。

但是计算机中,我们是以byte为单位来存放的,那么110 0001只需要一个字节,填满8位就是0110 0001;101 0100 0011 0100需要两个字节,填满16位就是0101 0100 0011 0100。这个过程就是所谓的utf-8编码过程。

他们之间的关系

A bitstring is a type that stores arbitrary number of bits, you can have a 5bit bitstring whereas binary stores arbitrary number of bytes

Here is some code that should make things clearer:

# bitstring
bs = << 3 :: size(2) >>      # => 2 bits 11
IO.inspect bs                # => <<3::size(2)>>
IO.inspect is_bitstring(bs)  # => true
IO.inspect is_binary(bs)     # => false

# binary
bin = << 3 >>                # => 8 bits or 1 byte
IO.inspect bin               # => <<3>>
IO.inspect is_bitstring(bin) # => true
IO.inspect is_binary(bin)    # => true

A binary is just a collection of bytes, so it has to have a number of bits that is divisible by 8 (i.e. a byte). So you can have a 8 bit binary, 16 bit binary and so on. If your binary is not divisible by 8, e.g. 7bits, 15bits, 14 bits, 23bits, you have a bitstring. And since a bitstring can have any number of bits even a binary is a bitstring. However, the inverse is not true.

杀手图来了

So a subset of bitstrings are binaries, and a subset of binaries are strings. Like this:

Elixir中的String、binaries和bitstring

转载于:https://my.oschina.net/u/855913/blog/1614208