Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing arabic string in c++

Tags:

c++

I'm beginner in C++

I'd like to print Arabic statement in C++ using Borland C, but I failed , I also tried to save the file as UTF-8 but it didn't help.

So please if any one knows anything about this or about what is the problem or how to configure the compiler to print Arabic please help me.

#include<iostram.h>
#include<conio.h>
void main()
{
   clrscr();
   char x [5] = {'ا','ح','م','د'};
   for(int i = 0; i< 5; i++)
       cout << x[i];
   getche();
}
like image 841
Ahmed Hamed Avatar asked Mar 18 '13 20:03

Ahmed Hamed


1 Answers

First of all, you are assuming that your source code can contain Arabic characters. This is a very bad idea, and depends on the assumption that the compiler is interpreting your source file in the same code page as your editor is writing it in.

The safest way to handle Arabic or other arbitrary Unicode in Windows C++ is to compile with _UNICODE, declare variables of wchar_t (and friends), and use Unicode constants like '\u6041' for your Arabic characters. If you must do things with 'char', you will have to come up with the multi-byte \x sequences in the right code page for your Arabic characters, and deal with the fact that a single char can't hold an Arabic character in UTF-8.

Finally, since you are using cout, this will only show you Arabic if the current code page of your DOS box is an Arabic code page.

like image 54
bmargulies Avatar answered Sep 22 '22 19:09

bmargulies