Sometimes we need to output non-english characters in Console, this is possible(really?) using Console.OutputEncoding property. Below is sample code to output latin & chinese characters
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace Test
{
class Program
{
static string latinString = "èé·ä»";
static string chineseString = "蘇雷什:密碼";
[DllImport("kernel32.dll")]
static extern bool SetConsoleOutputCP(uint wCodePageID);
static void Main(string[] args)
{
var latinEncoding = Encoding.GetEncoding("ISO-8859-1");
var utf8Encoding = Encoding.GetEncoding("UTF-8");
//-------Start Chinese Characters------------
SetConsoleOutputCP((uint)utf8Encoding.CodePage);
Console.OutputEncoding = utf8Encoding;
Console.WriteLine(chineseString);
//-------End Chinese Characters------------
//-------Start Latin Characters------------
SetConsoleOutputCP((uint)latinEncoding.WindowsCodePage);
Console.OutputEncoding = latinEncoding;
Console.WriteLine(latinString);
//-------End Latin Characters------------
}
}
}
But wait - it ain't going to display on windows(English) OS.
http://stackoverflow.com/questions/6405428/can-i-get-console-to-show-chinese
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/fe549b29-3e09-4176-b317-1e0094abf71a/
http://www.csharp-examples.net/culture-names/
http://stackoverflow.com/questions/2213541/vietnamese-character-in-net-console-application-utf-8
Wednesday, 29 February 2012
Friday, 24 February 2012
Wednesday, 15 February 2012
Useful functions - contd.....
public static void PrintHexString(byte data)
{
Console.WriteLine("Hex of {0} is {1}", data, BitConverter.ToString(BitConverter.GetBytes(data)));
}
-----------------------
private static void DemoNegativeNumberStorageInBinary()
{
Console.WriteLine("Binary Representation of {0} - {1}", 1000, Convert.ToString(1000, 2));
/* how negative is done
* short value = -7;
* 7 = 0000 0000 0000 0111
* Not (7) = 1111 1111 1111 1000
* Add 1 = 1111 1111 1111 1000
* +
* 1
* 1111 1111 1111 1001
* (1)111 1111 1111 1001 - is negative because it is 1
*
* -7 + 7 = 0
* 1111 1111 1111 1001 (+)
* 0000 0000 0000 0111
* 0000 0000 0000 0000
*/
Console.WriteLine("Binary Representation of {0} - {1}", -1000, Convert.ToString(-1000, 2));
}
-----------------------
{
Console.WriteLine("Hex of {0} is {1}", data, BitConverter.ToString(BitConverter.GetBytes(data)));
}
-----------------------
private static void DemoNegativeNumberStorageInBinary()
{
Console.WriteLine("Binary Representation of {0} - {1}", 1000, Convert.ToString(1000, 2));
/* how negative is done
* short value = -7;
* 7 = 0000 0000 0000 0111
* Not (7) = 1111 1111 1111 1000
* Add 1 = 1111 1111 1111 1000
* +
* 1
* 1111 1111 1111 1001
* (1)111 1111 1111 1001 - is negative because it is 1
*
* -7 + 7 = 0
* 1111 1111 1111 1001 (+)
* 0000 0000 0000 0111
* 0000 0000 0000 0000
*/
Console.WriteLine("Binary Representation of {0} - {1}", -1000, Convert.ToString(-1000, 2));
}
-----------------------
Unicode and Encoding
Just for my reference
http://csharpindepth.com/Articles/General/Unicode.aspx
http://stackoverflow.com/questions/496321/utf8-utf16-and-utf32
http://stackoverflow.com/questions/643694/utf-8-vs-unicode
In nutshell
1. .Net, by default use UTF - 16, and Encoding.Unicode means UTF - 16.
2. UTF8: Variable-width encoding, backwards compatible with ASCII. ASCII characters (U+0000 to U+007F) take 1 byte, code points U+0080 to U+07FF take 2 bytes, code points U+0800 to U+FFFF take 3 bytes, code points U+10000 to U+10FFFF take 4 bytes. Good for English text, not so good for Asian text.
3. UTF16: Variable-width encoding. Code points U+0000 to U+FFFF take 2 bytes, code points U+10000 to U+10FFFF take 4 bytes. Bad for English text, good for Asian text.
4. UTF32: Fixed-width encoding. All code points take 4 bytes. An enormous memory hog, but fast to operate on. Rarely used.
http://csharpindepth.com/Articles/General/Unicode.aspx
http://stackoverflow.com/questions/496321/utf8-utf16-and-utf32
http://stackoverflow.com/questions/643694/utf-8-vs-unicode
In nutshell
1. .Net, by default use UTF - 16, and Encoding.Unicode means UTF - 16.
2. UTF8: Variable-width encoding, backwards compatible with ASCII. ASCII characters (U+0000 to U+007F) take 1 byte, code points U+0080 to U+07FF take 2 bytes, code points U+0800 to U+FFFF take 3 bytes, code points U+10000 to U+10FFFF take 4 bytes. Good for English text, not so good for Asian text.
3. UTF16: Variable-width encoding. Code points U+0000 to U+FFFF take 2 bytes, code points U+10000 to U+10FFFF take 4 bytes. Bad for English text, good for Asian text.
4. UTF32: Fixed-width encoding. All code points take 4 bytes. An enormous memory hog, but fast to operate on. Rarely used.
Wednesday, 8 February 2012
SQL Server DateTime vs .Net DateTime
Just for reference
http://seesharper.wordpress.com/2008/07/08/sql-server-datetime-vs-net-datetime-battle-of-accuracy/
http://stackoverflow.com/questions/1021755/compare-two-datetime-values-from-sql-server-with-c-sharp/1021838#1021838
http://codebetter.com/petervanooijen/2008/08/21/valid-date-time-values-in-sql-server-sqldatetime-vs-datetime/
http://seesharper.wordpress.com/2008/07/08/sql-server-datetime-vs-net-datetime-battle-of-accuracy/
http://stackoverflow.com/questions/1021755/compare-two-datetime-values-from-sql-server-with-c-sharp/1021838#1021838
http://codebetter.com/petervanooijen/2008/08/21/valid-date-time-values-in-sql-server-sqldatetime-vs-datetime/
Monday, 30 January 2012
Abstract Factory vs Factory
There are numerous post out there to explain the differences - i just wanted to add on
Factory - encapsulates the logic of creating a concrete type
Abstract Factory - encapsulates grouping of related factories.
I always start with Factory pattern first to decouple the creation logic from client.
So when will we go for Abstract Factory - as soon as your factory method violates Open/Closed principle. If your Factory method contains lots of 'if/else' or 'switch' statements, then it is time to move onto Abstract Factory. Please refer this article.
Also DBProviderFactory is another example of AF pattern. SqlClientFactory, OracleClientFactory, OleDbFactory are all grouped under DBProviderFactory.
http://en.wikipedia.org/wiki/Abstract_factory_pattern
http://en.wikipedia.org/wiki/Factory_object
http://msdn.microsoft.com/en-us/library/dd0w4a2z%28v=vs.80%29.aspx
Factory - encapsulates the logic of creating a concrete type
Abstract Factory - encapsulates grouping of related factories.
I always start with Factory pattern first to decouple the creation logic from client.
So when will we go for Abstract Factory - as soon as your factory method violates Open/Closed principle. If your Factory method contains lots of 'if/else' or 'switch' statements, then it is time to move onto Abstract Factory. Please refer this article.
Also DBProviderFactory is another example of AF pattern. SqlClientFactory, OracleClientFactory, OleDbFactory are all grouped under DBProviderFactory.
http://en.wikipedia.org/wiki/Abstract_factory_pattern
http://en.wikipedia.org/wiki/Factory_object
http://msdn.microsoft.com/en-us/library/dd0w4a2z%28v=vs.80%29.aspx
Labels:
Design
Location:
Bengaluru, Karnataka, India
Tuesday, 24 January 2012
Modifying Web.Config at run time
Sample code to update an app setting in web.config file at run time. I assume the user have physical path of the web.config, instead of virtual directory.
private static void ChangeAppSettting(string oldValue, string newValue)
{
string appSettingName = "your app setting name";
string configFilePath = "Your web.config path";
Configuration configuration = OpenConfigFile(configFilePath );
AppSettingsSection appSettings = configuration.AppSettings;
if (appSettings != null && appSettings.Settings[appSettingName] != null)
{
appSettings.Settings[AppSetting].Value = newValue;
configuration.Save();
}
}
private static Configuration OpenConfigFile(string configPath)
{
FileInfo configFile = new FileInfo(configPath);
VirtualDirectoryMapping virtualDirectoryMapping = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
var webConfigFileMap = new WebConfigurationFileMap();
webConfigFileMap.VirtualDirectories.Add("/", virtualDirectoryMapping);
return WebConfigurationManager.OpenMappedWebConfiguration(webConfigFileMap, "/");
}
private static void ChangeAppSettting(string oldValue, string newValue)
{
string appSettingName = "your app setting name";
string configFilePath = "Your web.config path";
Configuration configuration = OpenConfigFile(configFilePath );
AppSettingsSection appSettings = configuration.AppSettings;
if (appSettings != null && appSettings.Settings[appSettingName] != null)
{
appSettings.Settings[AppSetting].Value = newValue;
configuration.Save();
}
}
private static Configuration OpenConfigFile(string configPath)
{
FileInfo configFile = new FileInfo(configPath);
VirtualDirectoryMapping virtualDirectoryMapping = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
var webConfigFileMap = new WebConfigurationFileMap();
webConfigFileMap.VirtualDirectories.Add("/", virtualDirectoryMapping);
return WebConfigurationManager.OpenMappedWebConfiguration(webConfigFileMap, "/");
}
Location:
Bengaluru, Karnataka, India
Subscribe to:
Posts (Atom)