简体   繁体   English

将文件转换为二进制C#

[英]converting file to binary c#

I want to convert file into binary. 我想将文件转换为二进制文件。 I tried, but I get 0X000000000000000..... It is not correct. 我尝试过,但是得到0X000000000000000 .....这是不正确的。 Always every file getting that digits. 始终每个文件都获得该数字。 Please help me to solve thanks in advance 请帮助我提前解决谢谢

if (value.resume_file.CompareTo("") != 0)
{
    byte[] binary = new byte[value.resume_file.Length];
    //binary =  Convert.ToByte(value.resume_file);
    objJobSeekers.IsResume = true;
    objJobSeekers.DocFileName = value.resume_file;
    objJobSeekers.Resume = binary;
    objJobSeekers.TypedResume = DBNull.Value;
}

string to byte[] 字符串到字节[]

byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);

byte[] to string byte []转换为字符串

str = System.Text.Encoding.UTF8.GetString(bytes);

Is this what you looking for? 这是您要找的东西吗?

If you want to read any file from disk and get its bytes then use 如果要从磁盘读取任何文件并获取其字节,请使用

string FileDir = "D:\\File.doc";
byte[] MyBytes = File.ReadAllBytes(FileDir);

If you want to convert some random object in memory into bytes then you can use a BinarySerializer 如果要将内存中的一些随机对象转换为字节,则可以使用BinarySerializer

byte[] MyBytes;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
    bf.Serialize(ms, obj);
    MyBytes = ms.ToArray();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM