[erlang-questions] Binaries

Hugo Mills hugo@REDACTED
Tue Jun 19 17:48:00 CEST 2018


On Tue, Jun 19, 2018 at 04:32:49PM +0100, Bob Cowdery wrote:
> If I have a number, say 1450000001 and I want to represent that as a
> binary in the form
> 
>  <<16#14,16#50,16#00,16#00,16#01>> what's the best way.
> 
> I'm not sure what list_to_binary(integer_to_list(1450000001)) which
> prints as <<"1450000001">> produces but I guess its 10 bytes not 5.

   This is basically just binary coded decimal, isn't it?

   You should be able to do this with a binary comprehension:

1> << <<(C-$0):4>> || C <- "12345678">>.
<<18,52,86,120>>

   Hugo.

-- 
Hugo Mills             | Great oxymorons of the world, no. 7:
hugo@REDACTED carfax.org.uk | The Simple Truth
http://carfax.org.uk/  |
PGP: E2AB1DE4          |
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180619/7df37ac9/attachment.bin>


More information about the erlang-questions mailing list