List of bits to String

*To*: mathgroup at smc.vnet.net*Subject*: [mg28726] List of bits to String*From*: "Helge Andersson" <helgea at inoc.chalmers.se>*Date*: Fri, 11 May 2001 03:38:44 -0400 (EDT)*Sender*: owner-wri-mathgroup at wolfram.com

Hello, In an earlier posting Murray Eisenberg wrote I have a list consisting of binary digits, of a given length, e.g., {0, 1, 1, 0, 1}. I wish to convert this to a _string_ consisting of those digits: "01101". How can this be done? Several people responded quick and fast. There were several approaches, some of them were fast and elegante, others not. I performed a competition with some of the postings. Two factors were considered, Length of the coding and the timing. Here is the result of the competition listed in order of performance. As you can see, the Length of coding happend to coincide with the timing even if not in proportion. The List of binary digits was called dli In[157]:= dli=Random[Integer,{0,1}]&/@Range[10000]; ___________________________________________ In[158]:= Timing[ToString[FromDigits[dli]];] Out[158]= {0.13 Second,Null} In[159]:= Timing[StringJoin[ToString/@dli];] Out[159]= {0.531 Second,Null} In[160]:= Timing[Apply[StringJoin,ToString/@dli];] Out[160]= {0.551 Second,Null} In[161]:= g[{x_,y_,z___}] := { StringJoin[ToString[x], ToString[y]], z }; f[ lst_ ]:=Nest[g, lst, Length[lst]-1]; In[162]:= Timing[f[dli];] Out[162]= {32.437 Second,Null}