# iconv EILSEQ error



## FestusHagen (Jan 4, 2013)

Hi all,

Working on a C project that uses iconv, it works properly on Fedora and Windows yet fails on FreeBSD-8.2.

Hoping someone can point me in the direction of a resolution.

The errno is 86, And is defined in /usr/local/include/iconv.h as:

```
EILSEQ        86        /* Illegal byte sequence */
```
The byte sequence appears correct, See below.

With the following info one would think it would work on FreeBSD and Win32, but not Fedora.

Systems info:

```
FreeBSD 8.2
#iconv --version
iconv (GNU libiconv 1.14)
Copyright (C) 2000-2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Bruno Haible.
```


```
WinXPsp3/Win7
C:\>iconv --version
iconv (GNU libiconv 1.14)
Copyright (C) 2000-2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Bruno Haible.
GNU libiconv v1.14
```


```
Fedora v?? (copy and paste from another users email)
#iconv --version
iconv (GNU libc) 2.15
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Ulrich Drepper.
```

Code frag:

```
char mb[8];
	char *outbuf = mb;
	size_t inlen = sizeof(wchar_t)/sizeof(char);
	size_t outlen = 8
	wchar_t &wc = THIS->operator[](i);
	const char *inbuf = (const char *)&wc;
	codec = iconv_open("UTF-8", "WCHAR_T");
	iconv(codec, NULL, NULL, NULL, NULL);
	if(iconv(codec, &inbuf, &inlen, &outbuf, &outlen) == -1)
```
FreeBSD gdb shows

```
(gdb) x inbuf
0x28ee2d10: 0x00007684
```
I do believe that to be the correct value ...

Thanks y'all.

-Enjoy
fh  : )_~


----------



## FestusHagen (Jan 4, 2013)

Simple test program.


```
#include <stdio.h>
#include <iconv.h>

// FreeBSD
// g++ -g -o iconv-test -I/usr/local/include -L/usr/local/lib iconv-test.cpp -liconv

// On Win32 I use MinGW32, I use Strawberry Perl so I use it's MinGW ...
// g++ -g -o iconv-test -IC:/Strawberry/c/include -LC:/Strawberry/c/lib iconv-test.cpp -liconv

#if defined(__FreeBSD__)
size_t iconv_wrap(iconv_t cd, char **inbuf, size_t *inbytesleft,
                  char **outbuf, size_t *outbytesleft)
{
  char *in = *inbuf;
  const char *in_c = (const char*)(in);
  return iconv(cd, &in_c, inbytesleft, outbuf, outbytesleft);
}
#else
size_t iconv_wrap(iconv_t cd, char **inbuf, size_t *inbytesleft,
                  char **outbuf, size_t *outbytesleft)
{
  return iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft);
}
#endif /* __FreeBSD__ */

int main(int argc, char* argv[])
{
  iconv_t codec;
  char mb[8];
  char *inbuf, *outbuf;
  size_t inlen, outlen;

  outlen = 8;
  outbuf = mb;
  inlen = sizeof(wchar_t)/sizeof(char);

  const wchar_t &wc = 30340;
  inbuf = (char *)&wc;

  codec = iconv_open("UTF-8", "WCHAR_T");
  if(!codec)
    printf("iconv_open failed.\n");

  iconv(codec, NULL, NULL, NULL, NULL);

  if(iconv_wrap(codec, &inbuf, &inlen, &outbuf, &outlen) == -1) {
    printf("iconv failed.\n");
    iconv_close(codec);
    return -1;
  }
  iconv_close(codec);

  mb[8-outlen] = '\0';
  for(int i = 0; i<8-outlen; i++)
    printf(":%x:", mb[i]);
  return 0;
}
```

-Enjoy
fh  : )_~


----------



## FestusHagen (Jan 4, 2013)

*[solved]  iconv EILSEQ error*

WCHAR_T is system dependent!
Use the encoding that created the String that the wchar_t &wc came from in the first place ... Duh!

However, I didn't know what that was, this is software written by ???
I do not understand all this utf / Unicode / Code sets / Wide Char / etc ... Just trying to fix something that's broke!
Yup, I know I need to do some serious educating on this topic, I know a bunch more today then I did 5 days ago!

If I would of just kept at it another two hours I wouldn't be wasting y'alls time.

Input is still welcome.

-Enjoy
fh  : )_~


----------

