GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Oct-2007, 07:14
rajeshgalla rajeshgalla is offline
New Member
 
Join Date: Oct 2007
Posts: 18
rajeshgalla is on a distinguished road
Red face

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


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.

CPP / C++ / C Code:
#include<stdio.h>
#include<wchar.h>

void convertMultiBytes(wchar_t *wchar, char *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 (aa, 0, (l + 1)*4);
convertMultiBytes(aa,c);
printf("Entered maultybyte string:%s\nConveted wild char string:%ls\n",c,aa);
}


void convertMultiBytes(wchar_t *wchar, char *mbyte)
{
   int n;
   char *end,*start;
   n = 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 *wchar, char *mbyte, int 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++;
       }
   }
}
Last edited by LuciWiz : 13-Oct-2007 at 13:07. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
multi byte to wide char converting problem(Related to Unicode) rajeshgalla C Programming Language 2 11-Oct-2007 07:03
Pointer initialization causing program abend? emanresu C Programming Language 0 12-Dec-2006 10:36
getting an error while compiling and running using different IDE. jaro C Programming Language 0 25-Aug-2006 09:14
Memory cannot be read? dlare9 C Programming Language 3 16-Nov-2005 07:03
Help with syntax errors PeteGallo C Programming Language 7 08-Aug-2005 20:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:52.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.