WTF with iconv (glibc 2.3.6.ds1-11)

Need help with C, C++, perl, python, etc?

WTF with iconv (glibc 2.3.6.ds1-11)

Postby apt-get » 2007-02-13 12:42

Please cast a glance at this code:

Code: Select all
#include <iconv.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>

int Iconv(iconv_t cd, char* out_buf, int out_bytes, char* in_buf)
    size_t inbytes = strlen(in_buf);
    size_t outbytes = out_bytes;

    int ret = iconv(cd, &in_buf, &inbytes, &out_buf, &outbytes);
    if(ret != 0) printf("\tERROR: convert failed\n");
    return ret;

int main (void)
    iconv_t cd = iconv_open("UTF8", "KOI8-R");
    if(cd == (iconv_t)-1)
        printf("\nCannot create iconv\n");
        return 0;

    char tmp[] = "This is a test string";
    char res[1024];

    printf("input length is %i\n", strlen(tmp));
    Iconv(cd, res, 1024, tmp);


    printf("%s\tlength is %i\n", res, strlen(res));

    return 0;

Output is:

Code: Select all
input length is 21
This is a test string[ goes non-printable characters...]  length is 35.

Length is not equal! Wtf?

I have tested this on Ubuntu (glibc 2.3.6-0ubuntu20) and it work Ok.

Return to Programming

Who is online

Users browsing this forum: No registered users and 8 guests