Click here to Skip to main content
14,162,626 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to get the bytes from a file then put them in the program and then convert it


1. string with the bytes code.
2. then convert it to exe file.

Please remember that the file needs to make it self without the orgnal file.

What I have tried:

System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
string str = enc.GetString(fileBytes);
streamWriter f2 = new StreamWriter("bytes.txt");
// I try to make it a txt file so that i could copy it in to a string
string bit = File.ReadAllText("bytes.txt");
byte[] array = Encoding.ASCII.GetBytes(bit);
File.WriteAllBytes("happy.exe", array);
// then i tried to make it a file again and that is where i falled.
Updated 15-Jul-18 11:41am
Richard MacCutchan 14-Jul-18 11:56am
Where do the original bytes come from? Unless they are the actual content from a .NET executable then your work is in vain, as is all your converting to and from strings and ASCII bytes.
WOLF 2018 14-Jul-18 12:01pm
they come from a exe file
Patrice T 14-Jul-18 12:04pm
You need to provide much more details.
Give an example.
WOLF 2018 14-Jul-18 12:07pm
i am using C# exe and i am trying to turn it in to a string that i can use on other programs i make. the file is called bit.exe. they not much info then that
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I'm pretty sure you are being treated as a "virus" by the OS (creating garbage exe's).
WOLF 2018 14-Jul-18 12:10pm
my antivirus is disabled. so thats not the case
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Don't read it as text: read it directly as bytes:
byte[] data = File.ReadAllBytes(pathToFile);
File.WriteAllBytes(pathToExe, data);

Data in ASCII can be just 7 bit, and different systems will interpret some of it in different ways (like \n and \r being "condensed" when you read them for example) so you may lose important information when you treat it as text.
WOLF 2018 14-Jul-18 12:02pm
i will try that now
WOLF 2018 14-Jul-18 12:04pm
i can't because the file the bytes are in is a text and it won't let me read it as bytes
OriginalGriff 14-Jul-18 12:12pm
That ... um ... turns out not to be the case.
Files are files: all files are a sequence of bytes and can be read as such, anything else is an "interpretation" of the bytes as a specific format, which is why reading byte data as text is a bad idea - the interpretation causes the data to change. The system will not prevent you at all from reading a file as bytes (provided you have the appropriate read permissions on the file and folder at all).

Use a hex editor and look at the "text" file and see exactly what it contains.
OriginalGriff 14-Jul-18 12:30pm
And that isn;t text - it's binary data, byte data. Read that as text, and you will lose most of the information - and it won't work as an EXE file at all.

BTW: that isn't a hex editor: this is what a Hex editor gives you:
See how much, much more readable it is?
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

i am using C# exe and i am trying to turn it in to a string that i can use on other programs i make. the file is called bit.exe. they not much info then that

If I understand, you want to embed a random exe in your program and probably silently run it as being yours.
Even if legal and fair, it will be a difficult task and you need a deep knowledge about the OS because the file on HDD is not what is run in memory, the os makes changes in it, and you will have to make those changes in order to run it.
byte[] array = Encoding.ASCII.GetBytes(bit);

In any case, read bytes, write bytes, never do a conversion.
WOLF 2018 15-Jul-18 17:11pm
yes but not i don't want it to run silently. I just thought if i could get the bytes in to a string, then i view the bytes and put them in another program so that it could use the bytes to make the file again. I know it sounds like a mind f*** but i think it could be done, or can you complie C# code from a file to make an exe without a complier. (sorry about my spelling.)
Patrice T 15-Jul-18 17:17pm
Are you trying to make a self extracting archive ?
WOLF 2018 15-Jul-18 17:17pm
yes. I want my program to check if a the file esists and if they don't then i want it to make them so that i don't have to download them.
Patrice T 15-Jul-18 17:22pm
It is very important to say it.

Use Improve question to update your question.
So that everyone can pay attention to this information.
WOLF 2018 15-Jul-18 17:27pm
ok well how would i do this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190518.1 | Last Updated 15 Jul 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100