Elixir中的String、binaries和bitstring
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:
转载于:https://my.oschina.net/u/855913/blog/1614208
上一篇: Tensorflow入门
下一篇: SMZX十日游(第二阶段模拟赛)
推荐阅读
-
Elixir中的String、binaries和bitstring
-
java 中的String 和new String 的区别 博客分类: JDK//Demo Stringnew String()
-
在Java中String和Date、Timestamp之间的转换
-
在Java中String和Date、Timestamp之间的转换
-
Java 中 String,StringBuffer 和 StringBuilder 的区别及用法
-
Java 中 String,StringBuffer 和 StringBuilder 的区别及用法
-
Java中实现String.padLeft和String.padRight的示例
-
java中 String和StringBuffer的区别实例详解
-
Java中Object.equals和String.equals的区别详解
-
java中 String和StringBuffer的区别实例详解