title: How can I efficiently transform a numpy.int8 array in-place to a value-shifted numpy.uint8 array?
body: <p>I have a large numpy array of signed bytes (<code>dtype int8</code>).  It contains values in the full range -128 to +127. I'd like to convert the efficiently to an array of unsigned bytes (<code>dtype uint8</code>) by adding 128 to each element, such that -128 → 0, 0 → 128, +127 → 255, <em>etc.</em> so of course the results still fit into an unsigned byte.</p>
  
  <p>Simple elementwise addition given the correct numerical result, but creates a result array using twice the memory (<code>dtype int16</code>) in addition to the source array, even though only the low bytes of the result elements are needed.</p>
  
  <pre><code>&gt;&gt;&gt; import numpy
  &gt;&gt;&gt; a = numpy.array( [-128, -1, 0, 1, 127 ], dtype=numpy.int8)
  &gt;&gt;&gt; b = a + 128
  &gt;&gt;&gt; b
  array([  0, 127, 128, 129, 255], dtype=int16)
  </code></pre>
  
  <p>Is there a way to control the <code>dtype</code> of the result array to be <code>uint8</code>?</p>
  
  <p>The alternative approach of modifying the values in-place and "casting" the data to a new type, like this:</p>
  
  <pre><code>&gt;&gt;&gt; for i in xrange(0, 5):
  ...     if a[i] &lt; 0:
  ...         a[i] -= 128
  ...     elif a[i] &gt;= 0:
  ...         a[i] += 128
  ...
  &gt;&gt;&gt; a
  array([   0,  127, -128, -127,   -1], dtype=int8)
  &gt;&gt;&gt; a.view(dtype=numpy.uint8)
  array([  0, 127, 128, 129, 255], dtype=uint8)
  </code></pre>
  
  <p>is much more space efficient but very costly in time for large arrays with the transformation in Python.</p>
  
  <p>How can I do this transformation in-place and quickly?</p>