1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

multi byte to wide char converting problem(Related to Unicode)

Discussion in 'C' started by harimagic, Oct 10, 2007.

Thread Status:
Not open for further replies.
  1. harimagic

    harimagic New Member

    Joined:
    Oct 5, 2007
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I prepared this program please go through it. i am running this program in linux with gcc compiler. i am giving input in the tmp.txt file with Telugu or Hindi characters.

    But I am not getting proper output. I found 2 problems:
    1. These characters are not converting to wide characters.
    2. On the console characters are printing with some junk value.

    Can any one help me to solve.

    PHP:
    #include<stdio.h>
    #include<wchar.h>

    void convertMultiBytes(wchar_t *wcharchar *mbyte);

    main()
    {
    FILE *fp;
    wchar_t *aa;
    char c[100];
    int l=0;
    if((
    fp=fopen("tmp.txt""r")) == NULL) {
        
    printf("Cannot open file.\n");
        exit(
    1);
      }
    fgets(c,25,fp);
    l=strlen(c);
    printf("%d",l);
    aa=(wchar_t *)malloc((l+1)*4);
    memset (aa0, (1)*4);
    convertMultiBytes(aa,c);
    printf("Entered maultybyte string:%s\nConveted wild char string:%ls\n",c,aa);
    }


    void convertMultiBytes(wchar_t *wcharchar *mbyte)
    {
       
    int n;
       
    char *end,*start;
       
    strlen(mbyte) + 1;
       
    /* convert multibyte string to wide character string */
       
    if (mbstowcs(wchar,mbyte,n) == (size_t)-1)
       {
           
    printf("ERROR: Could not convert multibyte character to wide char code\n");
           
    convertBytebyByte(wchar,mbyte,n);
       }
    }
    void convertBytebyByte(wchar_t *wcharchar *mbyteint n)
    {
       
    int mbcnt;
       
    char *end,*start;
       
    end mbyte n;
       
    start mbyte;

       for(;
    start <= end;)
       {
           
    mbcnt mbtowc(wchar,start,20);
           if(
    mbcnt 0)
           {
               
    start += mbcnt;
               
    wchar++;
           }
           else
           {
               
    printf("Invalid multibyte character occured\n");
               
    printf("Converting the invalid character to '?'\n");
               *
    wchar '?';
               
    start++;
           }
       }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
Thread Status:
Not open for further replies.

Share This Page