8

How to write unicode character farsi in c++ in ms-dos?

cout<<"Helo world"<<"سلام جهان";

Bob
  • 22,810
  • 38
  • 143
  • 225
Samiey Mehdi
  • 9,184
  • 18
  • 49
  • 63

4 Answers4

5
#include <iostream>
#include <locale>
#include <string>

int main()
{
      using namespace std;
      wstring wcs = L"中文";
      locale old = wcout.imbue(locale("") ); // "" is environment's default locale
      wcout<<wcs<<endl;
      wcout.imbue(old );                     // restore old locale
}
OwnWaterloo
  • 1,963
  • 14
  • 11
  • Doesn't answer the question: this writes wholly different characters :P – Thomas Jan 30 '10 at 19:25
  • 1
    @Thomas: But unless you are completely brain dead (which I hope is not true for all programmers). The ability to change the above program to print out different characters will be (how should I put it) "A braid dead cut and paste". The whole point is to set the correct local (since the default is the "C" locale which is usualy not very good at anything but English). – Martin York Jan 30 '10 at 19:44
  • 1
    Don't forget to use a font that contains the letters of the desired langauge. Also, Farsi like Arabic is RTL, some letters are connected other are not and what not. Good luck trying to "fix" the stupid win console to do what you want ;) – Khaled Alshaya Jan 30 '10 at 19:50
  • @Martin:are you claiming that the "C" locale *is* good at English? If so, I'd beg leave to disagree... – Jerry Coffin Jan 30 '10 at 21:57
  • 1
    @Jerry: No. My sentence is very explicit and that is not what it says. – Martin York Jan 30 '10 at 22:01
3

You need to convert the string to proper codepage. MS-DOS codepages are named like CP437, with CP followed by three digits. Notice that normally only less than 256 different characters may be displayed and thus many Unicode characters just cannot be shown in text mode.

Tronic
  • 10,250
  • 2
  • 41
  • 53
  • CP708 is the Arabic code page in MS-DOS. See http://msdn.microsoft.com/en-us/library/cc195051.aspx for the full list from Microsoft. – David Jan 30 '10 at 23:55
1
using namespace std;
setlocale(LC_ALL, "fa-IR");

time_t rawtime;
struct tm * timeinfo;

time(&rawtime);
timeinfo = localtime(&rawtime);

struct lconv * lc;
lc = localeconv();


LOGFONT * tFont;
tFont = GetFont();

CFont font;
VERIFY(font.CreateFont(
    tFont->lfHeight,                    // nHeight
    tFont->lfWidth,                     // nWidth
    0,                                  // nEscapement
    0,                                  // nOrientation
    tFont->lfWeight,                    // nWeight
    FALSE,                              // bItalic
    FALSE,                              // bUnderline
    0,                                  // cStrikeOut
    ARABIC_CHARSET,                     // nCharSet
    OUT_DEFAULT_PRECIS,                 // nOutPrecision
    CLIP_DEFAULT_PRECIS,                // nClipPrecision
    ANTIALIASED_QUALITY,                // nQuality
    DEFAULT_PITCH | FF_SWISS,           // nPitchAndFamily
    tFont->lfFaceName));            // lpszFacename 

const wchar_t yek = L'\u06F1';
const wchar_t dow = L'\u06F2';
const wchar_t seh = L'\u06F3';
const wchar_t chahar = L'\u06F4';
const wchar_t panj = L'\u06F5';
const wchar_t shish = L'\u06F6';
const wchar_t haft = L'\u06F7';
const wchar_t hasht = L'\u06F8';
const wchar_t noh = L'\u06F9';
const wchar_t sefr = L'\u06F0';

wchar_t wFATime[20];
wcscpy(wFATime, L"");

BOOL dayoftheweek = FALSE;
for (unsigned int i = 0; i<m_strText.GetLength(); i++)
{
    char c = m_strText[i]; //this is your character


    switch (c)
    {
    case '1':
        wcsncat(wFATime, &yek, 1);
        break;
    case '2':
        wcsncat(wFATime, &dow, 1);
        break;
    case '3':
        wcsncat(wFATime, &seh, 1);
        break;
    case '4':
        wcsncat(wFATime, &chahar, 1);
        break;
    case '5':
        wcsncat(wFATime, &panj, 1);
        break;
    case '6':
        wcsncat(wFATime, &shish, 1);
        break;
    case '7':
        wcsncat(wFATime, &haft, 1);
        break;
    case '8':
        wcsncat(wFATime, &hasht, 1);
        break;
    case '9':
        wcsncat(wFATime, &noh, 1);
        break;
    case '0':
        wcsncat(wFATime, &sefr, 1);
        break;
    case ':':
        wcsncat(wFATime, L":", 1);
        break;
    case '.':
        wcsncat(wFATime, L".", 1);
        break;
    case '/':
        wcsncat(wFATime, L"/", 1);
        break;
    default:
        dayoftheweek = TRUE;
    }
}

CFont* def_font = pdc->SelectObject(&font);
HDC hdc = pdc->GetSafeHdc();

int old = pdc->SetBkMode(TRANSPARENT);
if (dayoftheweek)
{
    CStringW strw(m_strText);
    ::TextOutW(hdc, m_poOffset.x, m_poOffset.y, strw, strw.GetLength());
}
else
{
    ::TextOutW(hdc, m_poOffset.x, m_poOffset.y, wFATime, wcslen(wFATime));
}



pdc->SelectObject(def_font);
pdc->SetBkMode(old);
// Done with the font.  Delete the font object.
font.DeleteObject();
gvlasov
  • 18,638
  • 21
  • 74
  • 110
0

you can use wchar_t instead of char.

and you should use wcout instead of cout too.

if your console support unicode this will work. another way for Iranians was a software called "vegaf" but its for much years ago and i forget it.

Behnam Safari
  • 2,941
  • 6
  • 27
  • 42