GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
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:11
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:05. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 11-Oct-2007, 03:42
seprich seprich is offline
Member
 
Join Date: Jun 2007
Posts: 110
seprich has a spectacular aura aboutseprich has a spectacular aura about

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


Quote:
Originally Posted by rajeshgalla
i am running this program in linux with gcc compiler.
ok there is one important piece of information.
Another important thing looking at your code is that you are making a command line application, not an x-windows application.

If you try to make commandline application and expect to see some writing in telugu scripts the task can be quite difficult. Even if you would succeed to tweak Your system to the right condition to display telugu, the same program will not display the same way in another computer. e.g. in my linux.

So first question is: what are your locale settings ?
if you write in your shell :
Code:
~ # echo $LANG
what is the answer you get ?

You must have locale that is capable displaying hindi and telugu. What I checked from other sources I found that for handling hindi that is devanagari writing the LANG should be "hi_IN.UTF-8" And for displaying telugu "te_IN.UTF-8". I really do not know if there exists locale which enables you to display both devanagari and telugu on commandline at the same time.

While it is possible to display various characters on command line I think x-windows would be much more suitable for displaying other-than-latin texts. So why not use x-windows ?

Quote:
Originally Posted by rajeshgalla
i am giving input in the tmp.txt file with Telugu or Hindi characters.

Formatted according to which standard ??
is it UTF-32 or perhaps UTF-16 ??
  #3  
Old 11-Oct-2007, 07:03
seprich seprich is offline
Member
 
Join Date: Jun 2007
Posts: 110
seprich has a spectacular aura aboutseprich has a spectacular aura about

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


update to the previous:
the following source (although the date is not very recent): http://www.ibiblio.org/pub/Linux/doc...ic-Fonts-HOWTO
claims in point 3.1 that you cannot get linux console to display devanagari characters.
So it would seem that X-windows app is the only practical choice.
 
 

Recent GIDBlogMeeting the populace by crystalattice

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
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
Converting a BYTE array to a string qrio12 C++ Forum 5 10-Jun-2006 09:42
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 05:24.


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