how to display text in the proper encoding?
My c# script:
using System;
using System.Text;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using fomm.Scripting;
class Script : FalloutNewVegasBaseScript {
public static bool install;
// PNH MENU SETTINGS
public static Form installPNHForm;
public static PictureBox backgroundPicture;
public static Button okButton;
public static Button cancelButton;
// Options buttons
public static RadioButton ReadiusRadioButton;
public static Label vanillaIconsLabel;
public static RadioButton Pipboy2005RadioButton;
public static Label pnhLabel;
//Options panel
public static Panel optionsPanel;
public static Label optionsLabel;
public static bool IsPluginActive(String pluginName) {
string[] loadOrder = GetActivePlugins();
for (int i = 0; i < loadOrder.Length; ++i) {
if (loadOrder[i].Equals(pluginName, StringComparison.InvariantCultureIgnoreCase)) {
return true;
}
}
return false;
}
public static Image GetImageFromFomod(string filename) {
byte[] data = GetFileFromFomod(filename);
MemoryStream s = new MemoryStream(data);
Image img = Image.FromStream(s);
s.Close();
return img;
}
public static void CreatePNHForm() {
setUpPNHForm();
setUpPNHBackgroundImage();
setUpPNHHelpertext();
setUpOptions();
setUpButtons();
AttachHandlers();
}
public static void setUpOptions() {
optionsPanel = new Panel();
optionsPanel.Location = new Point(10, 180);
optionsPanel.Size = new Size(267, 50);
optionsPanel.BackColor = Color.WhiteSmoke;
optionsPanel.BorderStyle = BorderStyle.FixedSingle;
backgroundPicture.Controls.Add(optionsPanel);
ReadiusRadioButton = new RadioButton();
ReadiusRadioButton.Checked = true;
ReadiusRadioButton.Location = new Point(15, 15);
ReadiusRadioButton.Size = new Size(20, 20);
vanillaIconsLabel = new Label();
vanillaIconsLabel.Text = "�����";
vanillaIconsLabel.Location = new Point(37, 19);
vanillaIconsLabel.AutoSize = true;
optionsPanel.Controls.Add(ReadiusRadioButton);
optionsPanel.Controls.Add(vanillaIconsLabel);
Pipboy2005RadioButton = new RadioButton();
Pipboy2005RadioButton.Checked = false;
Pipboy2005RadioButton.Location = new Point(168, 16);
Pipboy2005RadioButton.Size = new Size(20, 20);
pnhLabel = new Label();
pnhLabel.Text = "Pipboy2005";
pnhLabel.Location = new Point(190, 19);
pnhLabel.AutoSize = true;
optionsPanel.Controls.Add(Pipboy2005RadioButton);
optionsPanel.Controls.Add(pnhLabel);
}
public static void setUpPNHHelpertext() {
Panel helperTextPanel = new Panel();
helperTextPanel.Location = new Point(60, 130);
helperTextPanel.Size = new Size(170, 30);
helperTextPanel.BackColor = Color.WhiteSmoke;
helperTextPanel.BorderStyle = BorderStyle.FixedSingle;
backgroundPicture.Controls.Add(helperTextPanel);
Label helperTextLabel = new Label();
helperTextLabel.Text = "Readius or Pipboy 2005?";
hel开发者_开发问答perTextLabel.Location = new Point(22, 5);
helperTextLabel.AutoSize = true;
helperTextPanel.Controls.Add(helperTextLabel);
}
public static void setUpPNHForm() {
installPNHForm = CreateCustomForm();
installPNHForm.FormBorderStyle = FormBorderStyle.Fixed3D;
installPNHForm.StartPosition = FormStartPosition.CenterScreen;
installPNHForm.MaximizeBox = false;
installPNHForm.Size = new System.Drawing.Size(300, 360);
installPNHForm.Text = "Readius vs Pipboy 2005";
}
public static void setUpPNHBackgroundImage() {
backgroundPicture = new PictureBox();
backgroundPicture.Location = new Point(0, 0);
backgroundPicture.Size = new Size(292, 327);
// load picture file from .fomod and stream into picture box
// add BackgroundPicture to list of controls
installPNHForm.Controls.Add(backgroundPicture);
}
public static void setUpButtons() {
okButton = new Button();
okButton.Text = "Install";
okButton.BackColor = Color.Silver;
okButton.Location = new Point(148, 286);
okButton.Size = new Size(130, 33);
// cancel button
cancelButton = new Button();
cancelButton.Text = "Cancel";
cancelButton.BackColor = Color.Silver;
cancelButton.Location = new Point(9, 286);
cancelButton.Size = new Size(130, 33);
backgroundPicture.Controls.Add(okButton);
backgroundPicture.Controls.Add(cancelButton);
}
public static void AttachHandlers() {
//Attach a handler that will fire when the apply button is clicked
okButton.Click += delegate(object sender, EventArgs args) {
install = true;
installPNHForm.Close();
};
cancelButton.Click += delegate(object sender, EventArgs args) {
install = false;
installPNHForm.Close();
};
}
public static bool OnActivate() {
CreatePNHForm();
installPNHForm.ShowDialog();
if (install) {
InstallMenu();
}
return install;
}
public static int GetPluginIndex(String pluginName)
{
string[] loadOrder = GetAllPlugins();
for (int i = 0; i < loadOrder.Length; ++i)
{
if (loadOrder[i].Equals(pluginName, StringComparison.InvariantCultureIgnoreCase))
return i;
}
return -1;
}
public static void PlaceAtPlugin(String targetPlugin, String pluginToMove, int offset)
{
int targetPluginIndex = GetPluginIndex(targetPlugin);
int pluginToMoveIndex = GetPluginIndex(pluginToMove);
if (targetPluginIndex != -1 && pluginToMoveIndex != -1)
{
int[] pluginIdxArray = { pluginToMoveIndex };
SetLoadOrder(pluginIdxArray, targetPluginIndex + offset);
}
}
public static void PlaceAfterPlugin(String targetPlugin, String pluginToMove)
{
PlaceAtPlugin(targetPlugin, pluginToMove, 1);
}
public static void PlaceBeforePlugin(String targetPlugin, String pluginToMove)
{
PlaceAtPlugin(targetPlugin, pluginToMove, 0);
}
public static void InstallMenu() {
if (ReadiusRadioButton.Checked) {
CopyDataFile("Readius_NV.bsa", "Readius_NV.bsa");
InstallFileFromFomod("Readius_NV.esp");
if (IsPluginActive("WMX-ArenovalisTextures.esp"))
{
PlaceAfterPlugin("WMX-ArenovalisTextures.esp", "Readius_NV.esp");
}
if (IsPluginActive("TheUltimateGhostOverhaul.esp"))
{
PlaceAfterPlugin("TheUltimateGhostOverhaul.esp", "Readius_NV.esp");
}
SetPluginActivation("Readius_NV.esp", true);
} else if (Pipboy2005RadioButton.Checked) {
CopyDataFile("Pipboy2500.bsa", "Pipboy2500.bsa");
InstallFileFromFomod("Pipboy2500.esp");
if (IsPluginActive("WMX-ArenovalisTextures.esp"))
{
PlaceAfterPlugin("WMX-ArenovalisTextures.esp", "Pipboy2500.esp");
}
if (IsPluginActive("TheUltimateGhostOverhaul.esp"))
{
PlaceAfterPlugin("TheUltimateGhostOverhaul.esp", "Pipboy2500.esp");
}
SetPluginActivation("Pipboy2500.esp", true);
}
}
}
"vanillaIconsLabel.Text = "????";", how to display this in 1251 codepage?. Thanks.
Basically, not in the way you'd like or, realistically, in a way that makes sense from a performance perspective.
.Net controls are Unicode, and accept Unicode strings.
You can read in a File in 1252 and get a string from it like the SO question here demonstrates:
How do I convert from a possibly Windows 1252 'ANSI' encoded uploaded file to UTF8 in .NET?
or in 1251:
Convert Latin 1 encoded UTF8 to Unicode
But it's not a good way to get values into a Label control.
You should convert your source text into Unicode and just let it all be native.
If you're going to consider supporting different languages, then you might want to investigate using the .net RESX system too.
It's difficult to tell exactly what's going on here: Is the program displaying the text incorrectly, or is it just VS?
I am guessing that it's just a problem with VS and that your text is in the right encoding, but it is not being displayed with the correct encoding in VS.
You can force VS to display a text file in a particular encoding in VS as follows:
right-click file -> Open With... -> CSharp Editor with Encoding -> { Choose Encoding }
Unless you know what encoding your file is in, you're going to have to do trial-by-error. A few to try:
(Auto-Detect)
Unicode (UTF-8 without signature) - Codepage 65001
Unicode - Codepage 1200
However, your file may not be Unicode, perhaps it is ANSI, which means you will have to chose a Codepage i.e. "language" to display it in. Do you have any ideas what this language may be?
If all else fails, perhaps you could attach the file somewhere, or point out where it can be downloaded, then we can have a look.
OK. Bitaweb.com/en/codeConverter.html did not help me. Then i open my code script.cs in notepad, saved in UTF-8 and the problem gone. The whole problem was within C# the editor, which retains only the ANSI, not UTF-8.
精彩评论