?
Solved

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

Posted on 2003-02-19
12
Medium Priority
?
257 Views
Last Modified: 2010-05-01
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
0
Comment
Question by:yamaraj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
12 Comments
 

Expert Comment

by:RoryR
ID: 7980824
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)
0
 

Author Comment

by:yamaraj
ID: 7989129
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)
0
 

Expert Comment

by:RoryR
ID: 8002522
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 :-)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:yamaraj
ID: 8003084
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
0
 
LVL 2

Expert Comment

by:JSMCM
ID: 8006695
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
0
 

Author Comment

by:yamaraj
ID: 8010022
its ok john.I'm already strting to get some intrest.
0
 
LVL 2

Expert Comment

by:JSMCM
ID: 8011998
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
0
 
LVL 2

Expert Comment

by:JSMCM
ID: 8012168
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"
0
 

Author Comment

by:yamaraj
ID: 8014791
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?
0
 
LVL 2

Accepted Solution

by:
JSMCM earned 400 total points
ID: 8015244
Private Sub Form_Load()
Dim Move() As Byte

Dim pp As Long

pp = FileLen("c:\tester.txt") ' filelen returns the length of the file in bytes

ReDim Move(pp + 1)  


Open "C:\tester.txt" For Binary Access Read As #1

Do While Not EOF(1)

On Error Resume Next
Get #1, x, Move(x)
Combo1.AddItem Move(x)
On Error GoTo 0
x = x + 1

Loop




Close #1

End Sub


==================================
==================================

I redim the byte array to the number of bytes returned by pp + 1. The plus 1 is very important because the program probably will have a terminator bit which will not be returned with the filelen function.

' in otherwords, your program will enter an infinite loop because the loop say, loop until eof is true.

'I then loop through in the same way but instead of adding bytes to another file, I just write them to a combo box (or whatever you choose).

'NOTE: This worked with a plain text file with the word Hello in it. It worked because the word Hello is only 5 bytes long. On the other hand, the simple program I spoke about previously which had a megbox "Hi" in it when compiled is about 15 000 bytes long, and an average sized jpg image was about 650 000 byte long. I am convinced that the combo box can't handle such long entries, and even if it could, I don't know if the array can hold it.

But, I think I'll leave that up to you to figure out, then please post your answer as I am also interested, but I really need to do some work now.
0
 

Author Comment

by:yamaraj
ID: 8027281
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.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question