将byte []转换为String并返回c#

时间:2012-10-09 13:31:02

标签: c# string byte

我正在尝试使用Encoding.Unicode将byte []转换为字符串并返回。 有时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");
    }
}

3 个答案:

答案 0 :(得分:6)

你不能使用编码:你必须使用像Convert.ToBase64String / Convert.FromBase64String这样的东西。

编码假设byte []根据特定规则进行格式化,而不是随机非字符串字节[]的情况。

总结:

编码将任意字符串转换为格式化字节[]

Base-64将任意字节[]转换为格式化字符串

答案 1 :(得分:0)

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

答案 2 :(得分: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");
            }