Steffen Nurpmeso dixit:
> #include <stdio.h>
> #include <string.h>
> #include <iconv.h>
> #include <errno.h>
> int main(void){
> char inb[16], oub[16], *inbp, *oubp;
> iconv_t id;
> size_t inl, oul;
>
> memcpy(inbp = inb, "a\303\244c", sizeof("a\303\244c"));
> inl = sizeof("a\303\244c") -1;
Not -1 otherwise oub will not be NUL-terminated and end with junk:
$ ./a.out
Converting 4 <aäc>
GOT <a?c<� e+x���g��aäc>
Without the trailing NUL, stateful conversation may also be
incomplete…
> oul = sizeof oub;
> oubp = oub;
>
> if((id = iconv_open("ascii", "utf8")) == (iconv_t)-1)
> return 1;
Throws 1 because you need "utf-8", but with it, see above.
> fprintf(stderr, "Converting %lu <%s>\n",(unsigned long)inl, inbp);
> if(iconv(id, &inbp, &inl, &oubp, &oul) == (size_t)-1){
> fprintf(stderr, "Fail <%s>\n", strerror(errno));
> return 2;
> }
> fprintf(stderr, "GOT <%s>\n", oub);
> iconv_close(id);
> return 0;
> }
>
>you should get replacement characters out of the box?
Citrus iconv agrees. Its manpage says:
If the string pointed to by *src contains a character which is valid
under the source codeset but can not be converted to the destination
codeset, the character is replaced by an "invalid character" which
depends on the destination codeset, e.g., '?', and the conversion is con-
tinued. iconv() returns the number of such "invalid conversions".