如何在Erlang中将整数转换为二进制? [英] How do I convert an integer to a binary in Erlang?

查看:84
本文介绍了如何在Erlang中将整数转换为二进制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将整数制成二进制:

I am trying to make an integer into a binary:

543 = <<"543">>

没有它怎么办?

integer_to_list(list_to_binary(K)).

推荐答案

如果您想将543转换为<<"543" >>,我认为您找不到比以下方法更快的东西了:

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">>

因为在这种情况下,这两个功能都在C中实现.

Because in this case both functions implemented in C.

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

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>>

这篇关于如何在Erlang中将整数转换为二进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆