簡體   English   中英

將byte []轉換為String並返回c#

[英]Converting byte[] to String and back c#

我正在嘗試將byte []轉換為字符串,然后使用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");
    }
}

您不能為此使用Encoding:必須使用類似Convert.ToBase64String / Convert.FromBase64String之類的東西。

編碼假定byte []是根據特定規則進行格式化的,而隨機非字符串byte []則不是這種情況。

總結一下:

編碼將任意字符串與已格式化的字節[/]相互轉換。

Base-64將任意字節[]轉換為格式化字符串或從格式化字符串轉換

you cannot use encoding use base64

使用base64可以安全地將字節轉換為字符串並返回

base64 guaranteed to not to get "invalid" unicode sequences例如:
替代對的前半部分沒有后半部分的用法如下:

string base64 = Convert.ToBase64String(bytes);
byte[] bytes = Convert.FromBase64String(base64);

這是一個示例,其中我更改了圖像並將其成像為位數組,然后將其轉換回可讀的字符串。

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");
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM