English 中文(简体)
C#中细微的穿附
原标题:Strict string to byte encoding in C#

I ve一只 st倒在ASCIIEncoding。 用黑体取代无效特性吗? 我对此感到非常困惑,因为这样一来就打破了至少令人惊讶的规则。 在Zalar,将使用u”some unicode string.encode(ascii),而转换则因违约而严格,因此,非ASCII特性将产生这一例外情况。

两个问题:

  1. How can strings be strictly converted to another encoding (like ASCII or Windows-1252), so that an exception is thrown if invalid characters occur? By the way I don t want a foreach loop converting each Unicode number to a byte, and then checking the 8th bit. This is supposed to be done by a great framework like .NET (or Python ^^).
  2. Any ideas on the rationale behind this default behavior? For me, it makes more sense to do strict conversions by default or at least define a parameter for this purpose (Python allows "replace", "ignore", "strict").
最佳回答

。 如果编码转换失败,网就可选择放弃一种例外情况。 页: 1 EncoderExceptionFallback category (throws a EncoderFallbackException if an content nature not be reflected to an encoded输出 byte序列) to establish an encoding. The following Code is from the documentation for that category:

Encoding ae = Encoding.GetEncoding(
              "us-ascii",
              new EncoderExceptionFallback(), 
              new DecoderExceptionFallback());

然后使用该编码进行转换:

// The input string consists of the Unicode characters LEFT POINTING 
// DOUBLE ANGLE QUOTATION MARK (U+00AB),  X  (U+0058), and RIGHT POINTING 
// DOUBLE ANGLE QUOTATION MARK (U+00BB). 
// The encoding can only encode characters in the US-ASCII range of U+0000 
// through U+007F. Consequently, the characters bracketing the  X  character
// cause an exception.

string inputString = "u00abXu00bb";
byte[] encodedBytes = new byte[ae.GetMaxByteCount(inputString.Length)];
int numberOfEncodedBytes = 0;
try
{
    numberOfEncodedBytes = ae.GetBytes(inputString, 0, inputString.Length, 
                                       encodedBytes, 0);
}
catch (EncoderFallbackException e)
{
    Console.WriteLine("bad conversion");
}

http://msdn.microsoft.com/en-us/library/ms404377.aspx”rel=“noreferer” MSDN page, “Character Encoding in the .NET Framework”,在一定程度上讨论了违约转换行为背后的理由。 简言之,他们不想淡化视这种行为的遗留应用。 他们确实建议推翻这一违约。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签