且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

整数到二进制Erlang

更新时间:2022-11-09 10:02:58

如果要将543转换为< 543>>我不认为你可以找到比以下更快的东西:

  1> list_to_binary(integer_to_list(543))。 
<543>>

因为在这种情况下,这两个函数都是在C中实现的。



如果要将整数转换为最小二进制表示形式,可以使用二进制:encode_unsigned 功能,新的二进制模块如下:

  1>二进制:encode_unsigned(543)。 
< 2,31>>
2> binary:encode_unsigned(543,little)。
< 31,2>>


I am trying to make an integer into a binary:

543 = <<"543">>

How can I do this without

integer_to_list(list_to_binary(K)).

If you want to convert 543 to <<"543">> I don't think you can find something faster than:

1> list_to_binary(integer_to_list(543)).
<<"543">>

Because in this case both functions implemented in C.

If you want to convert integer to the smallest possible binary representation you can use binary:encode_unsigned function from the new binary module like this:

1> binary:encode_unsigned(543).
<<2,31>>
2> binary:encode_unsigned(543, little).
<<31,2>>