[erlang-questions] bit syntax

7stud <>
Fri Dec 14 03:35:10 CET 2012


Hi,

=========
-----Original Message-----
From: "Erik Søe Sørensen" []
Date: 11/30/2012 04:06 AM
To: 
Subject: Re: [erlang-questions] bit syntax


As a workaround you can do this

> <<Length, Rest/binary>> = X.
> ValueLen = Length*8.
> <<Value:ValueLen>> = Rest.

Or this:

 2> <<Length,Value:Length/unit:8>> = X.
<<2,1,0>>
3> {Length,Value}.
{2,256}


Multiplication of a length by a constant is a special case which is supported through the unit qualifier.
==========

Thanks for the examples.


===========
76> << [49, 50, 51, 52] >>.
** exception error: bad argument

77> X = "1234".
"1234"

78> <<X>>.
** exception error: bad argument

The only way you can do this is

list_to_binary([49, 50, 51, 52]).
and
list_to_binary("1234").
 
The thing is that <<"ABC">> is *syntactic sugar* for <<65,66,67>>; this (literal strings) is a special case, and it is a *syntactic* special case
==============

Okay, thanks.



More information about the erlang-questions mailing list