title: Encode/compress sequence of repeating integers
body: <p>I have very long integer sequences that look like this (arbitrary length!): </p>
  
  <pre><code>0000000001110002220033333
  </code></pre>
  
  <p>Now I need some algorithm to convert this string into something compressed like </p>
  
  <pre><code>a9b3a3c3a2d5
  </code></pre>
  
  <p>Which means "a 9 times, then b 3 times, then a 3 times" and so on, where "a" stands for 0, "b" for 1, "c" for 2 and "d" for 3. </p>
  
  <p>How would you do that?
  So far nothing suitable came to my mind, and I had no luck with google because I didn't really know what to search for. What is this kind of encoding / compression called? </p>
  
  <p>PS: I am going to do the encoding with <strong>PHP</strong>, and the decoding in <strong>JavaScript</strong>.</p>
  
  <p><strong>Edit</strong>: Thank you all! </p>
  
  <p>I ended up with this function for encoding:</p>
  
  <pre><code>protected function numStringToRle($s){          
          $rle    = '';
          $count = 1;
          $len    = strlen($s);
          for($i = 0; $i &lt; $len; $i++){
              if($i != $len &amp;&amp; isset($s[$i+1]) &amp;&amp; $s[$i] == $s[$i+1]){
                  $count++;                
              } else {
                  $rle .= chr($s[$i] + 97).( $count == 1 ? '' : $count);                                
                  $count = 1;
              }
          }
          return $rle;            
  }
  </code></pre>
  
  <p>And that for decoding: </p>
  
  <pre><code>var decodeCoords = function(str) {
  
     str = str.replace(/(.)(\d+)/g, function(_, x, n) {
         return new Array(parseInt(n, 10) + 1).join(x);
     });
  
     return str.
       replace(/a/g, '0').
       replace(/b/g, '1').
       replace(/c/g, '2').
       replace(/d/g, '3');     
  };
  </code></pre>