title: Converting UTF-8 iso-8859 bytes - String.to_charlist
body: <p>I have run into an odd problem.  I have a program that receives data in bytes from an external source, which I in return perform some business logic and send a reply. We have come into an odd issue with String.to_charlist. </p>
  
  <p>Try this in iex: </p>
  
  <pre><code>String.to_charlist &lt;&lt;169, 99, 111, 114, 94, 51, 94, 51, 94, 66, 52, 57, 49, 49, 49, 32, 32, 49, 48, 51, 53, 94, 67, 79, 77, 80, 76, 69, 84, 69, 68, 94, 49, 49, 52, 50, 52, 53, 94, 75&gt;&gt;
  </code></pre>
  
  <p>This will throw the following error: </p>
  
  <pre><code>** (UnicodeConversionError) invalid encoding starting at &lt;&lt;169, 99, 111, 114, 94, 51, 94, 51, 94, 66, 52, 57, 49, 49, 49, 32, 32, 49, 48, 51, 53, 94, 67, 79, 77, 80, 76, 69, 84, 69, 68, 94, 49, 49, 52, 50, 52, 53, 94, 75&gt;&gt;
      (elixir) lib/string.ex:2035: String.to_charlist/1
  </code></pre>
  
  <p>Now if you strip off the first byte and in turn try: </p>
  
  <pre><code>String.to_charlist &lt;&lt;99, 111, 114, 94, 51, 94, 51, 94, 66, 52, 57, 49, 49, 49, 32, 32, 49, 48, 51, 53, 94, 67, 79, 77, 80, 76, 69, 84, 69, 68, 94, 49, 49, 52, 50, 52, 53, 94, 75&gt;&gt;
  </code></pre>
  
  <p>You get: </p>
  
  <blockquote>
    <p>'cor^3^3^B49111  1035^COMPLETED^114245^K'</p>
  </blockquote>
  
  <p>Is there a different way I should be converting these bytes to a string? I understand some characters like 169 may not be displayable, but what is the recommended way of handling such? </p>
  
  <p>Thanks for your help. </p>
  
  <p>I did find this: </p>
  
  <pre><code>&lt;&lt;169 :: utf8, 0&gt;&gt; 
  </code></pre>
  
  <p>Returns &lt;&lt;194, 169, 0>> which added the 194. So if you paste this into iex the output appears to be correct. </p>
  
  <pre><code>&lt;&lt;194, 169, 99, 111, 114, 94, 51, 94, 51, 94, 66, 52, 57, 49, 49, 49, 32, 32, 49, 48, 51, 53, 94, 67, 79, 77, 80, 76, 69, 84, 69, 68, 94, 49, 49, 52, 50, 52, 53, 94, 75&gt;&gt; 
  </code></pre>
  
  <p>Do I need to write a function that loops through the bytes and calls &lt;> then reduces on the bytes returned (except for the 0 concat)? </p>