开发者

converting array of Unicode chars to an array of ASCII characters [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

So what I understand of this is that a Unicode character is two bytes long, so the first byte should be an ASCII token and the second byte should be another ASCII token. I have an array of Unicode chars and I want to convert 开发者_高级运维it to an array of ASCII characters that will end up being twice as long as the original.


Joel says it best here:

http://www.joelonsoftware.com/articles/Unicode.html

I highly recommend giving this a read. It's the best primer on Unicode and character sets I've seen.


Sounds like you just want to split the Unicode bytes into two ASCII characters. The strings will be unrelated, the characters won't match at all.

Unicode characters are not made up of two ASCII tokens.

Unicode is a distinct encoding from ASCII.

But if you just want the byte data: Encoding.Unicode.GetBytes(data); is all you need.


You can use the Encoding.Convert method. With it you can specify the encodings from and to which you wish to convert your string (or array of chars).

As seen in their documentation there's this example:

using System; using System.Text;

namespace ConvertExample
{
   class ConvertExampleClass
   {
      static void Main()
      {
         string unicodeString = "This string contains the unicode character Pi(\u03a0)";

         // Create two different encodings.
         Encoding ascii = Encoding.ASCII;
         Encoding unicode = Encoding.Unicode;

         // Convert the string into a byte[].
         byte[] unicodeBytes = unicode.GetBytes(unicodeString);

         // Perform the conversion from one encoding to the other.
         byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);

         // Convert the new byte[] into a char[] and then into a string.
         // This is a slightly different approach to converting to illustrate
         // the use of GetCharCount/GetChars.
         char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
         ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
         string asciiString = new string(asciiChars);

         // Display the strings created before and after the conversion.
         Console.WriteLine("Original string: {0}", unicodeString);
         Console.WriteLine("Ascii converted string: {0}", asciiString);
      }
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜