erlang基础

来源:互联网 发布:网络建设 ppt 编辑:程序博客网 时间:2024/06/03 23:05

数值类型

整形

整形的表达式:

[+/-]Base#Value

这个Base值是2~16之间的数字,表示数字的进制。

浮点型

1.234E-10

原子

以小写字母开头的名称而是符号常量,它们被称为原子(atom)。

布尔值

操作符 描述 and Returns true only if both arguments are true andalso Shortcut evaluation of and: returns false if the first argument isfalse, without evaluating the second or Returns true if either of the arguments is true orelse Shortcut evaluation of or: returns true if the first argument istrue, without evaluating the second xor “Exclusive or”: returns true if one of its arguments is true and the other false not Unary negation operator: returns true if its argument is false, and vice versa

元组

如果想把一些数量固定的项目归组成单一的实体,就会使用元组(tuple)。创建元组的方法是用大括号把想要表示的值括起来,并用逗号分隔它们。举个例子,如果想要表示某人的名字和身高,就可以用{joe, 1.82}。这个元组包含了一个原子和一个浮点数。

变量

Erlang的变量以大写字母开头。所以X、This和A_long_name都是变量。

比特语法

编写服务器的时候基本是需要解析binary的数据。这个就是去搞定从内存到short,integer,long之类的变量。
类型指定列表里的各项可以有如下这些值。
* End可以是big | little | native
它指定机器的字节顺序。native是指在运行时根据机器的CPU来确定。默认值是big,也
就是网络字节顺序(network byte order)。这一项只和从二进制型里打包和解包整数与浮
点数有关。当你在不同字节顺序的机器上打包和解包二进制型里的整数时,应当注意设置
正确的字节顺序。
编写位语法表达式时,可能有必要先做些试验。为了确定自己没有弄错,你可以尝试下面
的shell命令:
输出结果展示了位语法是如何把整数打包进二进制型的。
如果你还是不放心,term_to_binary和binary_to_term可以帮你搞定打包和解包整数
的工作。因此,你可以在高位优先(big-endian)的机器上创建一个包含整数的元组,然
后用term_to_binary把它转换成二进制型并发送至低位优先(little-endian)的机器。最
后,在低位优先的机器上运行binary_to_term,这样元组里所有整数的值都会是正确的。
* Sign可以是signed|unsigned
这个参数只用于模式匹配。默认值是unsigned。
* Type可以是integer|float|binary|bytes|bitstring|bits|utf8|utf16|utf32
默认值是integer。
* Unit的写法是unit:1|2|…256
integer、float和bitstring的Unit默认值是1,binary则是8。utf8、utf16和utf32
类型无需提供值。

binary实例

操作符 描述 DOWND 32/unsigned-little-integer WORD 16/unsigned-little-integer float 16/little-float

反序列化一块内存出来:

 <<A:8/bitstring,B:8>> = <<59704:16>>.

序列化一块内存:

<<E:3,F:20>> = <<6351258:23/little>>.
13> << A:8/native,B:8/native,C:8/native,D:8/native >> = << 1470912052:32/native >>.<<"4V¬W">>14> A.5215> B.8616> C.17217> D.87
0 0
原创粉丝点击