Erlang二进制字节长度

问题描述 投票:16回答:1

我有二进制例如:

<<32,16,10,9,108,111,99,97,108,104,111,115,116,16,170,31>>

我怎么知道这个包的长度是多少?

谢谢。

erlang byte packets
1个回答
29
投票

对于字节大小:

1> byte_size(<<32,16,10,9,108,111,99,97,108,104,111,115,116,16,170,31>>).
16

对于位大小:

2> bit_size(<<32,16,10,9,108,111,99,97,108,104,111,115,116,16,170,31>>).
128

当你有一个位串(一个位长不能被字节大小为8整除的二进制数)时,byte_size/1会向上舍入到最接近的整个字节。即位串适合的字节数:

3> bit_size(<<0:19>>).
19

4> byte_size(<<0:19>>). % 19 bits fits inside 3 bytes
3

5> bit_size(<<0:24>>).
24

6> byte_size(<<0:24>>). % 24 bits is exactly 3 bytes
3

7> byte_size(<<0:25>>). % 25 bits only fits inside 4 bytes
4
© www.soinside.com 2019 - 2024. All rights reserved.