Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to print Unicode characters(Hindi) in a Console Application.

I tried :
C#
static void Main(string[] args)
{
    Console.WriteLine("दीपक");
    Console.ReadKey();
}

But output was ???? instead of दीपक.


Thanks
Posted

 
Share this answer
 
Comments
VJ Reddy 17-Apr-12 12:39pm    
Good reference. 5!
Shahin Khorshidnia 17-Apr-12 12:41pm    
Thanks a lot VJ :)
The solution to your problem explained in details at this link[^].
The code from this blog is here (I have nothing to do with this code, it is simply cut/paste):

C#
using System;
using System.Runtime.InteropServices;

public class Test 
{
    public static void Main() 
    {
        WriteLineRight("दीपक");
    }

    internal static bool IsConsoleFontTrueType(IntPtr std) {
     CONSOLE_FONT_INFO_EX cfie = new CONSOLE_FONT_INFO_EX();
        cfie.cbSize = (uint)Marshal.SizeOf(cfie);
        if(GetCurrentConsoleFontEx(std, false, ref cfie)) {
            return(((cfie.FontFamily & TMPF_TRUETYPE) == TMPF_TRUETYPE));
        }
        return false;
    }

    public static void WriteLineRight(string st) 
    {
        IntPtr stdout = GetStdHandle(STD_OUTPUT_HANDLE);
        if(stdout != INVALID_HANDLE_VALUE) {
            uint filetype = GetFileType(stdout);
            if(! ((filetype == FILE_TYPE_UNKNOWN) && (Marshal.GetLastWin32Error() != ERROR_SUCCESS))) {
                bool fConsole;
                uint mode;
                uint written;
                filetype &= ~(FILE_TYPE_REMOTE);
                if (filetype == FILE_TYPE_CHAR) {
                    bool retval = GetConsoleMode(stdout, out mode);
                    if ((retval == false) && (Marshal.GetLastWin32Error() == ERROR_INVALID_HANDLE)) {
                        fConsole = false;
                    } else {
                        fConsole = true;
                    }
                } else {
                    fConsole = false;
                }

                if (fConsole) {
                    if (IsConsoleFontTrueType(stdout)) {
                        WriteConsoleW(stdout, st, st.Length, out written, IntPtr.Zero);
                    } else {
                        //
                        // Not a TrueType font, so the output may have trouble here
                        // Need to check the codepage settings
                        //
                        // TODO: Add the old style GetConsoleFallbackUICulture code here!!!
                    }
                } else {
                    //
                    // Write out a Unicode BOM to ensure proper processing by text readers
                    //
                    WriteFile(stdout, BOM, 2, out written, IntPtr.Zero);
                    WriteFile(stdout, st, st.Length * 2, out written, IntPtr.Zero);
                }
            }
        }
    }

    [DllImport("kernel32.dll", CharSet=CharSet.Unicode, ExactSpelling=true)]
    internal static extern bool WriteConsoleW(IntPtr hConsoleOutput,                                               string lpBuffer,                                               int nNumberOfCharsToWrite, 
                                              out uint lpNumberOfCharsWritten,                                               IntPtr lpReserved);

    [DllImport("kernel32.dll", CharSet=CharSet.Unicode, ExactSpelling=true)]
    internal static extern bool WriteFile(IntPtr hFile,                                           string lpBuffer,                                           int nNumberOfBytesToWrite, 
                                          out uint lpNumberOfBytesWritten,                                           IntPtr lpOverlapped);

    [DllImport("kernel32.dll", ExactSpelling=true, SetLastError=true)]
    internal static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);

    [DllImport("kernel32.dll", ExactSpelling=true)]
    internal static extern bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput,                                                        bool bMaximumWindow,                                                         ref CONSOLE_FONT_INFO_EX lpConsoleCurrentFontEx);

    [DllImport("Kernel32.DLL", ExactSpelling=true, SetLastError=true)]
    internal static extern uint GetFileType(IntPtr hFile);

    [DllImport("Kernel32.DLL", ExactSpelling=true)]
    internal static extern IntPtr GetStdHandle(int nStdHandle);

    internal struct COORD {
        internal short X;
        internal short Y;
        internal COORD(short x, short y) {
            X = x;
            Y = y;
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    internal unsafe struct CONSOLE_FONT_INFO_EX {
        internal uint cbSize;
        internal uint nFont;
        internal COORD dwFontSize;
        internal int FontFamily;
        internal int FontWeight;
        fixed char FaceName[LF_FACESIZE];
    }

    internal const int TMPF_TRUETYPE = 0x4;
    internal const int LF_FACESIZE = 32;
    internal const string BOM = "\uFEFF";
    internal const int STD_OUTPUT_HANDLE = -11; // Handle to the standard output device.
    internal const int ERROR_INVALID_HANDLE = 6;
    internal const int ERROR_SUCCESS = 0;
    internal const uint FILE_TYPE_UNKNOWN = 0x0000;
    internal const uint FILE_TYPE_DISK = 0x0001;
    internal const uint FILE_TYPE_CHAR = 0x0002;
    internal const uint FILE_TYPE_PIPE = 0x0003;
    internal const uint FILE_TYPE_REMOTE = 0x8000;
    internal static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900