MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Re: list of digits to string

  • To: mathgroup at smc.vnet.net
  • Subject: [mg33818] RE: [mg33774] Re: list of digits to string
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Fri, 19 Apr 2002 02:27:36 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

> -----Original Message-----
> From: Allan Hayes [mailto:hay at haystack.demon.co.uk]
To: mathgroup at smc.vnet.net
> Sent: Tuesday, April 16, 2002 9:50 AM
> To: mathgroup at smc.vnet.net
> Subject: [mg33818] [mg33774] Re: list of digits to string
> 
> 
> "Murray Eisenberg" <murraye at attbi.com> wrote in message
> news:a8rgcs$b3s$1 at smc.vnet.net...
> > How do I convert a list of bits, say {0, 1, 1, 0, 0}, to a 
> corresponding
> > character string, "01100"?  Just for bits, the expression
> >
> >   StringJoin[lis /. {0 -> "0", 1 -> "1"}]
> >
> > does what I want.  But, more generally, how do I do the same with
> > arbitrary digits?  The expression similar to the above but for all 9
> > digits 0, 1, 2, ..., 9 seems overly long.  Is there an easier way?
> 
> 
> Murray,
> Initial zeros are a problem. Here are two ways:
> 
>     lst= Join[{0},Table[Random[Integer,{0,9}],{10000}]];
> 
>     s1=StringDrop[ToString[FromDigits[Prepend[lst,1]]],1];//Timing
> 
>         {1.1 Second,Null}
> 
>     s2=StringJoin@@ToString/@lst;//Timing
> 
>         {2.69 Second,Null}
> 
>     s1===s2
> 
>     True
> 
> --
> Allan
> 
> ---------------------
> Allan Hayes
> Mathematica Training and Consulting
> Leicester UK
> www.haystack.demon.co.uk
> hay at haystack.demon.co.uk
> Voice: +44 (0)116 271 4198
> Fax: +44 (0)870 164 0565
> 
> 
> 
> 

A short note to add: another way to avoid the problems with leading zeros is
to use PaddedForm.
 
In[11]:= lst = Join[{0, 0, 0}, Table[Random[Integer, {0, 9}], {10000}]];

In[12]:= Take[lst, 10]
Out[12]= {0, 0, 0, 7, 8, 3, 1, 1, 7, 3}

In[13]:= Length[lst]
Out[13]= 10003

In[14]:=
s3 = StringTake[
        ToString[
          PaddedForm[FromDigits[lst], 10003, 
            NumberPadding -> {"0", ""}]], -10003]; // Timing
Out[14]=
{0.661 Second, Null}

In[15]:= StringLength[s3]
Out[15]= 10003

In[16]:= StringTake[s3, 10]
Out[16]= "0007831173"

The timing is that same as from Allan's method

In[17]:= s1 = StringDrop[ToString[FromDigits[Prepend[lst, 1]]], 1]; //
Timing
Out[17]= {0.661 Second, Null}

You might ask why I StringTook[..., -10003]. This was not neccessary in the
case above, but at least one zero will always appear at the left, so it is
needed here

In[18]:= lst[[1]] = 1;
In[19]:=
s4 = StringTake[
      ToString[PaddedForm[FromDigits[lst], 10003, 
          NumberPadding -> {"0", ""}]], -10003];

In[20]:= StringTake[s4, 10]
Out[20]= "1007831173"

--
Hartmut



  • Prev by Date: RE: Passing arguments and pattern matching
  • Next by Date: Re: Bug in NSolve?
  • Previous by thread: Re: list of digits to string
  • Next by thread: Re: huge number, ciphers after decimal point?