1

バイト[]を文字列に変換し、Encoding.Unicodeを使用して元に戻そうとしています。Encoding.Unicodeがbyte[]を文字列に変換できる場合もあれば、出力が!=入力である場合もあります。私は何が間違っているのですか?

ご協力いただきありがとうございます。

public static void Main(string[] args)
{
    Random rnd = new Random();
    while(true)
    {
        Int32 random = rnd.Next(10, 20);
        Byte[] inBytes = new Byte[random];
        for(int i = 0; i < random; i++)
            inBytes[i] = (Byte)rnd.Next(0, 9);

        String inBytesString = Encoding.Unicode.GetString(inBytes, 0, inBytes.Length);
        Byte[] outBytes = Encoding.Unicode.GetBytes(inBytesString);

        if(inBytes.Length != outBytes.Length)
            throw new Exception("?");
        else
        {
            for(int i = 0; i < inBytes.Length; i++)
            {
                if(inBytes[i] != outBytes[i])
                    throw new Exception("?");
            }
        }
        Console.WriteLine("OK");
    }
}
4

3 に答える 3

6

そのためにエンコーディングを使用することはできません。Convert.ToBase64String/Convert.FromBase64Stringのようなものを使用する必要があります。

エンコーディングは、byte[]が特定のルールに従ってフォーマットされていることを前提としています。これはランダムな非文字列byte[]には当てはまりません。

要約すると:

エンコーディングは、フォーマットされたバイトとの間で任意の文字列を変換します[]

Base-64は、任意のbyte[]をフォーマットされた文字列との間で変換します

于 2012-10-09T13:33:01.990 に答える
0

これは、イメージをビット配列に変更し、それを読み取り可能な文字列に変換した例です。

protected bool isImageCMYK(HttpPostedFile image, Stream fileContent)
    {
            //creating byte array
        byte[] imageToByteArray = new byte[image.ContentLength];

            //filling the byte array
        fileContent.Read(imageToByteArray, 0 , image.ContentLength);

            //convering byte array back to a readable string
        UTF8Encoding byteToString = new UTF8Encoding();
        string imageString = byteToString.GetString(imageToByteArray);

        return imageString.ToLower().Contains("cmyk");
    }

これは、「OK」の出力をもたらす編集済みのコードです。

public static void Main(string[] args)
        {
            Random rnd = new Random();
            while (true)
            {
                Int32 random = rnd.Next(10, 20);
                Byte[] inBytes = new Byte[random];
                for (int i = 0; i < random; i++)
                    inBytes[i] = (Byte)rnd.Next(0, 9);

                UTF8Encoding inBytesString = new UTF8Encoding(); 
                string byteString = inBytesString.GetString(inBytes, 0, inBytes.Length);
                //Byte[] outBytes = Encoding.Unicode.GetBytes(inBytesString);
                Byte[] outBytes = inBytesString.GetBytes(byteString);

                if (inBytes.Length != outBytes.Length)
                    throw new Exception("?");
                else
                {
                    for (int i = 0; i < inBytes.Length; i++)
                    {
                        if (inBytes[i] != outBytes[i])
                            throw new Exception("?");
                    }
                }
                Console.WriteLine("OK");
            }
于 2012-10-09T13:46:58.173 に答える
0
you cannot use encoding use base64

u を使用base64すると、安全にバイトを文字列に変換して元に戻すことができます

base64 guaranteed to not to get "invalid" unicode sequencesのように:
後半の使用のないサロゲート ペアの前半は次のように:

string base64 = Convert.ToBase64String(bytes);
byte[] bytes = Convert.FromBase64String(base64);
于 2012-10-09T13:34:51.297 に答える