Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Unicode Console
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 22:02pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello
 
Look at this: Console Code Pages
 
Maybe this helps: C#: How to print a unicode string to console?
  Permalink  
Comments
VJ Reddy at 17-Apr-12 12:39pm
   
Good reference. 5!
Shahin Khorshidnia at 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
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,078
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 17 Apr 2012
Copyright © CodeProject, 1999-2014
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