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

harimagic's Avatar, Join Date: Oct 2007
Go4Expert Member
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 Code:
#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++;
       }
   }

0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
The second one which is again duplicate of multi byte to wide char converting problem(Related to Unicode). Please do not have the same thread over and over again. It irritates others which in turn reduces the chances of getting any positive response.