title: C convert iso ISO−8859-1 to UTF-8 with libconv
body: <p>I need to encode Latin ISO−8859-1 chars into to UTF-8 (and the reverse operation too). </p>
  
  <p>I first used this answer <a href="https://stackoverflow.com/questions/11156473/is-there-a-way-to-convert-from-utf8-to-iso-8859-1">Is there a way to convert from UTF8 to iso-8859-1?</a> to perform the operation and it works;</p>
  
  <p>Now I want to use the libiconv which provides all the conversion mechanisms and should help me to keep my code simpler.</p>
  
  <p>I have followed the example provided here:
  <a href="https://www.lemoda.net/c/iconv-example/iconv-example.html" rel="nofollow noreferrer">https://www.lemoda.net/c/iconv-example/iconv-example.html</a></p>
  
  <p>and I wrote a method that looks like this:</p>
  
  <pre><code>char *iconvISO2UTF8(char *iso) {
      iconv_t iconvDesc = iconv_open ("ISO−8859-1", "UTF-8//TRANSLIT//IGNORE");
  
      if (iconvDesc == (iconv_t) - 1) {
          /* Something went wrong.  */
          if (errno == EINVAL)
              fprintf(stderr, "conversion from '%s' to '%s' not available", "ISO−8859−1", "UTF-8");           
          else
              fprintf(stderr, "LibIcon initialization failure");          
  
          return NULL;
      }
  
      size_t iconv_value;
      char * utf8;
      size_t len;
      size_t utf8len; 
      char * utf8start;
  
      int len_start;
  
  
      len = strlen (iso);
      if (! len) {        
          fprintf(stderr, "iconvISO2UTF8: input String is empty.");           
          return NULL;
      }
  
      /* Assign enough space to put the UTF-8. */
      utf8len = 2 * len;
      utf8 = calloc (utf8len, sizeof (char));
      if (! utf8) {
          fprintf(stderr, "iconvISO2UTF8: Calloc failed.");           
          return NULL;
      }
      /* Keep track of the variables. */
      utf8start = utf8;
      len_start = len;
  
      iconv_value = iconv (iconvDesc, &amp; iso, &amp; len, &amp; utf8, &amp; utf8len);
      /* Handle failures. */
      if (iconv_value == (size_t) - 1) {      
          switch (errno) {
                  /* See "man 3 iconv" for an explanation. */
              case EILSEQ:
                  fprintf(stderr, "iconv failed: Invalid multibyte sequence, in string '%s', length %d, out string '%s', length %d\n", iso, (int) len, utf8start, (int) utf8len);             
                  break;
              case EINVAL:
                  fprintf(stderr, "iconv failed: Incomplete multibyte sequence, in string '%s', length %d, out string '%s', length %d\n", iso, (int) len, utf8start, (int) utf8len);              
                  break;
              case E2BIG:
                  fprintf(stderr, "iconv failed: No more room, in string '%s', length %d, out string '%s', length %d\n", iso, (int)  len, utf8start, (int) utf8len);                              
                  break;
              default:
                  fprintf(stderr, "iconv failed, in string '%s', length %d, out string '%s', length %d\n", iso, (int) len, utf8start, (int) utf8len);                             
          }
          return NULL;
      }
  
  
      if(iconv_close (iconvDesc) != 0) {
          fprintf(stderr, "libicon close failed: %s", strerror (errno));          
      }
  
      return utf8start;
  
  }
  </code></pre>
  
  <p>When I call this fonction with plain old ascii-like characters, like "abracadabra", iconv works.
  But as soon as I send accentuated chars to it, like "éàèüöä' then the iconv() call fails with a EILSEQ code:</p>
  
  <blockquote>
    <p>iconv failed: Invalid multibyte sequence, in string 'éàèüöä', length
    6, out string '', length 12</p>
  </blockquote>
  
  <p>Here is a sample main program that crash when stored in a source file encoded with ISO−8859-1 and compiled on a linux system with ISO−8859-1 as default charset:</p>
  
  <pre><code>int main(int argc, char **argv) {
      char *iso1 = "abracadabra";
      char *utf = iconvISO2UTF8(iso1);
      puts(utf);
      free(utf);
  
      char *iso2 = "éàèüöä";
      utf = iconvISO2UTF8(iso2);
      puts(utf);
      free(utf);
  }
  </code></pre>
  
  <p>Is it possible to run this kind of conversion with iconv ? 
  If yes what's wrong in this code ?</p>