Click here to Skip to main content
12,946,816 members (52,337 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi,

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

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

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


Thanks
Posted 15-Apr-12 21:02pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
VJ Reddy 17-Apr-12 12:39pm
   
Good reference. 5!
Shahin Khorshidnia 17-Apr-12 12:41pm
   
Thanks a lot VJ :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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):

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);
}
  Permalink  
v2

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,919
CHill60 3,235
Maciej Los 2,508
Jochen Arndt 1,935
ppolymorphe 1,795


Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 17 Apr 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100