使用字符编码转换为另一种格式
Convert in Another Format With Character Encoding
我正在为 C#
应用程序使用 Oracle 10g
数据库。问题是,在数据库中,NVARCHAR 列不保存英语以外的其他语言。由于 NVARCHAR 支持 Unicode,这应该可以工作。但是我尝试了一种使用教程的简单方法,如下所示:
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;
//Convert the string into a byte[].
byte[] unicodeBytes = ascii.GetBytes("আমার সোনার বাংলা!"); //Text to show
//Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(ascii, unicode, unicodeBytes);
char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string asciiString = new string(asciiChars);
Console.WriteLine(asciiString);
Console.ReadKey();
可能看起来很傻,但期待我是否可以在控制台应用程序中以上述格式显示文本。现在它显示问号 (???????)。我可以以任何方式显示文本并至少以任何其他格式保存它,这样我就可以在前端适当地检索和显示它。
如果你可以使用 unicode(你应该,嘿,现在是 2018 年),那么最好完全避免使用 Bijoy。处理和存储所有字符串,如 .NET 中的 System.String
和 Oracle 中的 NVARCHAR
。
Windows 控制台可以毫无问题地处理 unicode,如果我们观察到文档明确指出的两个重要先决条件:
Support for Unicode [...] requires a font that has the glyphs needed to render that character. To successfully display Unicode characters to the console, the console font must be set to a [...] font such as Consolas or Lucida Console
这是您必须在 Windows 设置中确保的,独立于您的 .NET 应用程序。
第二个先决条件,强调我的:
[...] Console class supports UTF-8 encoding [...] Beginning with the .NET Framework 4.5, the Console class also supports UTF-16 encoding [...] To display Unicode characters to the console. you set the OutputEncoding property to either UTF8Encoding or UnicodeEncoding.
文档没有说明的是,可以从控制台 window 的属性菜单中选择的字体 none 通常包含世界上所有字母的字形。如果您需要从右到左的功能,例如希伯来语或阿拉伯语,那您就不走运了。
如果程序是运行没有预装东亚字体的Windows版本,follow this tutorial安装Bangla LanguageInterfacePack (KB3180030)。
然后将this answer应用于我们的问题如下:
- 打开 windows 注册表编辑器
- 导航到 HKLM\Software\Microsoft\WindowsNT\CurrentVersion\Console\TrueTypeFont
- 创建一个新的字符串值,分配一个可用的键,如“000”,以及值 "Bangla Medium"
- 重启电脑
现在将控制台字体设置为 "Bangla",使用控制台的 window 菜单,最后一个菜单项 "Properties",第二个选项卡 "Font"。
终于摆脱所有来回编码,简单地写:
using System;
using System.Text;
namespace so49851713
{
class Program
{
public static void Main()
{
var mbb = "\u263Aআমার সোনার বাংলা!";
/* prepare console (once per process) */
Console.OutputEncoding = UTF8Encoding.UTF8;
Console.WriteLine(mbb);
Console.ReadLine();
}
}
}
我正在为 C#
应用程序使用 Oracle 10g
数据库。问题是,在数据库中,NVARCHAR 列不保存英语以外的其他语言。由于 NVARCHAR 支持 Unicode,这应该可以工作。但是我尝试了一种使用教程的简单方法,如下所示:
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;
//Convert the string into a byte[].
byte[] unicodeBytes = ascii.GetBytes("আমার সোনার বাংলা!"); //Text to show
//Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(ascii, unicode, unicodeBytes);
char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string asciiString = new string(asciiChars);
Console.WriteLine(asciiString);
Console.ReadKey();
可能看起来很傻,但期待我是否可以在控制台应用程序中以上述格式显示文本。现在它显示问号 (???????)。我可以以任何方式显示文本并至少以任何其他格式保存它,这样我就可以在前端适当地检索和显示它。
如果你可以使用 unicode(你应该,嘿,现在是 2018 年),那么最好完全避免使用 Bijoy。处理和存储所有字符串,如 .NET 中的 System.String
和 Oracle 中的 NVARCHAR
。
Windows 控制台可以毫无问题地处理 unicode,如果我们观察到文档明确指出的两个重要先决条件:
Support for Unicode [...] requires a font that has the glyphs needed to render that character. To successfully display Unicode characters to the console, the console font must be set to a [...] font such as Consolas or Lucida Console
这是您必须在 Windows 设置中确保的,独立于您的 .NET 应用程序。
第二个先决条件,强调我的:
[...] Console class supports UTF-8 encoding [...] Beginning with the .NET Framework 4.5, the Console class also supports UTF-16 encoding [...] To display Unicode characters to the console. you set the OutputEncoding property to either UTF8Encoding or UnicodeEncoding.
文档没有说明的是,可以从控制台 window 的属性菜单中选择的字体 none 通常包含世界上所有字母的字形。如果您需要从右到左的功能,例如希伯来语或阿拉伯语,那您就不走运了。
如果程序是运行没有预装东亚字体的Windows版本,follow this tutorial安装Bangla LanguageInterfacePack (KB3180030)。
然后将this answer应用于我们的问题如下:
- 打开 windows 注册表编辑器
- 导航到 HKLM\Software\Microsoft\WindowsNT\CurrentVersion\Console\TrueTypeFont
- 创建一个新的字符串值,分配一个可用的键,如“000”,以及值 "Bangla Medium"
- 重启电脑
现在将控制台字体设置为 "Bangla",使用控制台的 window 菜单,最后一个菜单项 "Properties",第二个选项卡 "Font"。
终于摆脱所有来回编码,简单地写:
using System;
using System.Text;
namespace so49851713
{
class Program
{
public static void Main()
{
var mbb = "\u263Aআমার সোনার বাংলা!";
/* prepare console (once per process) */
Console.OutputEncoding = UTF8Encoding.UTF8;
Console.WriteLine(mbb);
Console.ReadLine();
}
}
}