Converting byte[] to String and back c#

孤者浪人 提交于 2020-01-05 04:39:15

问题


I’m trying to convert a byte[] to a string and back using Encoding.Unicode. Sometimes Encoding.Unicode is able to convert the byte[] to a string and sometimes the output is != the input. What am I doing wrong?

Thanks for your help.

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

回答1:


You cannot use Encoding for that: you must use something like Convert.ToBase64String / Convert.FromBase64String.

Encoding assumes the byte[] is formatted according to specific rules, which are not the case for a random non-string byte[].

To summarise:

An Encoding turns an arbitrary string to/from a formatted byte[]

Base-64 turns an arbitrary byte[] to/from a formatted string




回答2:


you cannot use encoding use base64

using base64 u can safely convert bytes to a string and back

base64 guaranteed to not to get "invalid" unicode sequences like:
first half of a surrogate pair without the second half use like this:

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



回答3:


Here is an example where I changed and image to a bit array and then converted it back to a readable string.

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

here is the edited code which results in an output of "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");
            }


来源:https://stackoverflow.com/questions/12801214/converting-byte-to-string-and-back-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!