将字节(文件)数组转换为字符串,然后将字符串转换为字节数组 [英] Convert an array of byte(file) into string and then string to array of byte

查看:127
本文介绍了将字节(文件)数组转换为字符串,然后将字符串转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在编写一个代码,它将字节数组(它来自文件)转换为字符串,然后再将字符串转换为字节数组并将其保存到新文件中。 ..



这是我的控制台类,它没有错误但结果不正确。





  class 程序
{
public static bool SaveData( string FileName, byte [] Data)
{
BinaryWriter Writer = null ;
string Name = FileName;

尝试
{
// 创建要写入文件的新流
Writer = new BinaryWriter(File.OpenWrite(Name) );

// Writer原始数据
Writer.Write(数据);
Writer.Flush();
Writer.Close();
}
catch
{
// < span class =code-comment> ...
return false ;
}

返回 true ;
}
静态 void Main( string [] args)
{
byte [] bytes = File.ReadAllBytes( @ C:\ Users \ UserName \Desktop\File.jpg);
string result = System.Text.Encoding.UTF8.GetString(bytes);
byte [] bytes2 = System.Text.Encoding.UTF8.GetBytes(result);

SaveData( @ C:\ Users \ UserName \Desktop \ File .jpg,bytes2);

}
}







为什么?!我怎么解决呢?!我认为在转换过程中有些字符(字节)会丢失。有没有办法将文件转换成字符串并再次使用它?!

tnx。

解决方案

您好,



您应该使用Base64编码。请查看 [ ^ ]文章。



问候,


为什么?

当您将字节值转换为Char(或反之亦然)时,您正在对它们应用转换:并非所有字节值都转换为char值,因此您会获得某些字符的默认值。并非所有字符都转换为不同的字节...



因此,如果你取字节,然后转换为字符串,然后将其转换回字节,机会非常好,您将无法再获得原始字节值 - 您甚至可能得不到相同的字节数!特别是对于像jpeg这样的文件类型,这可能会完全破坏你的形象...



你认为这是一个好主意你想做什么?

Hi ,
I am writing a code which convert an array of byte (it is from a file) to string , then convert string into array of byte again and save it into a new file ...

this is my Console Class , It work without error but result is not correct .


class Program
{
    public static bool SaveData(string FileName, byte[] Data)
    {
        BinaryWriter Writer = null;
        string Name = FileName;

        try
        {
            // Create a new stream to write to the file
            Writer = new BinaryWriter(File.OpenWrite(Name));

            // Writer raw data
            Writer.Write(Data);
            Writer.Flush();
            Writer.Close();
        }
        catch
        {
            //...
            return false;
        }

        return true;
    }
    static void Main(string[] args)
    {
        byte[] bytes = File.ReadAllBytes(@"C:\Users\UserName\Desktop\File.jpg");
        string result = System.Text.Encoding.UTF8.GetString(bytes);
        byte[] bytes2 = System.Text.Encoding.UTF8.GetBytes(result);

        SaveData(@"C:\Users\UserName\Desktop\File.jpg", bytes2);

    }
}




why ?! how can I solve it ?! I think some of characters(byte) lose during this convert . Is there any way to convert a file into string and use it another time ?!
tnx.

解决方案

Hello,

You should be using Base64 encoding instead. Please have a look at this[^] article.

Regards,


Why?
When you convert Byte values to Char (or vice versa) you are applying a translation to them: not all byte values translate to char values, so you get "defaulted" values for some characters. And not all characters translate to distinct bytes either...

So if you take bytes, convert then to a string, and then convert that back to bytes, the chances are very good that you won't get the original byte values back again - you may not even get the same number of bytes! Particularly with a file type such as jpeg, this can completely ruin your image...

What are you trying to do that you think this is a good idea?


这篇关于将字节(文件)数组转换为字符串,然后将字符串转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆