Link to home
Start Free TrialLog in
Avatar of yamaraj
yamaraj

asked on

how do i load the bytes of a file into an array?

suppose i've got a file c:\temp\sick.exe
how do i load it into a array
and then display the bytes in a list box?

after that how to a make another file by using the same array of bytes.
thanks in advance
som
Avatar of RoryR
RoryR

well what ur wanting 2 do makes no sense. explain btr & i can help.

becoz:
a) y do u want 2 display an .exe? it cums out garbled
b) y a list box? y not a txt box?
c) if it was an .exe it wudnt fit in a list box
d) if u write an other file from that its just a copy. u nvr sed u where gonna alter ne of it. so just use file copy command

explain alittle abbout y & wot ur doing plz.

but if u want 2 load this may help

open "c:\temp\sick.exe" for binary as 1#
dim str as string*1 '(1 or what ever size u want 2 read data off)
do
get 1#,,str
sumthing=sumthing & str
loop until eof(1)
Avatar of yamaraj

ASKER

lets take the *.exe part out. I just want to know how i can load the bytes of a file into an array and <optional> show the data in the array in a textbox or a listbox. It doesn't matter whether it comes &*%^&% doesn't matter.

the thing is i load it up in an array then display the data and make an exact copy from the array.(maybe add 2 to each byte to change the file.Then when opening minus 2 from each byte and display the data)
erm.... sounds like u want 2 keep data hiden? ive got a btr way... but first i really need 2 know exactly what & y u want 2 do this... coz there are so many ways :-) (and u do want the best)

eg INI, random access, binary access, comma seperated

so just tell me what exactly u want & i'll write the code :-)
Avatar of yamaraj

ASKER

ok!
i've been reading stuff on encryption and compression so i wanted to try something out.
first the program loads the bytes of a file into a array.
then add a cetain value to each bytes like 2 or 3 then make a new file with this new array of bytes.Wow now its encrypted. then i  make a program that does the same thing but decreases the same value and eureka the same file.
hope you've understood what i'm trying to do.I kinda made in to a story.
thanks
Hi there guys, I've been following this question and I actually have a question of my own. Sorry for hi-jacking you question yamaraj, but maybe it will be of interest to you as well.

as yamaraj asked, can you take a file and get it's data byte by byte, and then use those bytes to transfer the file over a network, Serial port, or internet??

PS. If your concerned about points I will gladly contribute to the answer that yamaraj accepts.

John
Avatar of yamaraj

ASKER

its ok john.I'm already strting to get some intrest.
Hi yamaraj,

Thanks. Ok, here's what I've come up with so far.

I created a simple VB exe called Test. All it was was just a form with one command button on it. In the button's click event was msgbox "Hi".

I compiled and saved to :C:\My Documents\TESTMOVE\ORIG\test.exe.

I also created an empty directory called DEST in the TESTMOVE directory.


Then I ran the loop below, and viola, the program was copied to the new folder. I ran it and it worked.

I will also try to see about encrypting like you mentioned.

Please post anything you found out, as I would like to hear if you found a different way.






Private Sub Form_Load()
Dim Move As Byte


Open "C:\My Documents\TESTMOVE\ORIG\test.exe" For Binary Access Read As #1
Open "C:\My Documents\TESTMOVE\DEST\test.exe" For Binary Access Write As #2


Do While Not EOF(1)
x = x + 1
On Error Resume Next
Get #1, x, Move
Debug.Print Move
Put #2, x, Move
Loop



Close #2
Close #1


MsgBox "Done" ' put this here because it takes a while and I wanted to know wgen it was done


End Sub
OK, spent a few more minutes on this

Here is the explanation:

1) I use the code below to move my file byte by byte. I then XOR each byte. I use XOR and I DON'T add something as you suggested because a byte's maximum value can be 255. If you try to add a number to a byte value of 255, you might get some unpredictable result (I actually did try it by adding 10 and then subtracting 10 and it worked fine - I just trust the XOR more because it doesn't mess with the amount of bits, there will always be 8).

2) Once the file transfer is complete, you can see the new EXE file in the DEST folder. It is not however a functional program and windows says it can't run it. This is expected because we've scrambled the program.

3) to unscramble things, I run the exact same code as below, except I  use the following to open the files:

Open "C:\My Documents\TESTMOVE\DEST\test.exe" For Binary Access Read As #1  'same folder dest
Open "C:\My Documents\TESTMOVE\DEST\test1.exe" For Binary Access Write As #2 'same folder dest

I have to copy the test.exe file to test1.exe, because if I try to copy it to test.exe, I am adding data into the file as fast as I am reading it and the program never quits the loop.

4) If I wanted to, I could add some code to the end of the unscrambling routine to delete test.exe (the non functional one), and rename test1.exe to test.exe. I would then end up with an exact matching copy of the program.

5) By the way, I put the debug.print statment in because I wanted to see the bytes to make sure I was getting the right info, but it slows the process alot.




Dim Move As Byte


Open "C:\My Documents\TESTMOVE\ORIG\test.exe" For Binary Access Read As #1
Open "C:\My Documents\TESTMOVE\DEST\test.exe" For Binary Access Write As #2


Do While Not EOF(1)
x = x + 1
On Error Resume Next
Get #1, x, Move
Debug.Print Move Xor 1
Put #2, x, Move Xor 1
Loop



Close #2
Close #1

MsgBox x
MsgBox "Done"
Avatar of yamaraj

ASKER

hi
thanks for the answer but i had another question related.Waht if i wanted to load the bytes into an array.How do i do that?
ASKER CERTIFIED SOLUTION
Avatar of JSMCM
JSMCM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yamaraj

ASKER

thanks
i tried it and works alright.
Now i'll go into huffman and learn about compressing.
Any links you may know where i can finde vb info for huffman.