reversible hex dump

New to FreeBASIC? Post your questions here.
deadrats
Posts: 7
Joined: Jul 12, 2008 23:47

reversible hex dump

Postby deadrats » Jul 13, 2008 0:12

hello everyone, i have a quick question: is it possible to create a reversible hex dump (or binary dump) using freebasic?

i know it's possible in C/C++ (and in fact have both the source and the apps to do it), and i was working on doing it with free pascal, but as i was looking over the language documentation of free basic it seemed like it should be much easier to do it with FB.

since i'm not too familar with FB, near as i could figure i would have to do something like:

DIM buffer AS STRING

OPEN "sample.file" FOR BINARY AS #1
OPEN "output.txt" FOR OUTPUT AS #2

buffer = LOF(sample.file)

GET #1, buffer

WRITE #2, HEX(buffer)

CLOSE #2
CLOSE #1

would the above code snippet give me a text file with a hex dump of "sample.file"? if so, reversing the hex dump would be similar but i would have to substitute PUT instead of GET, also would i need to put the above in some kind of do..while not eof loop?

any help is appreciated.
counting_pine
Site Admin
Posts: 6200
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Jul 13, 2008 0:44

A simple hex dumping program would look something like this in FB:

Code: Select all

Dim buffer As String, buflen As integer

Open "sample.file" For Binary As #1
Open "output.txt" For Output As #2
   
    buflen = Lof(1)
    buffer = Space(buflen)
   
    Get #1, 1, buffer
   
   
    For i As Integer = 0 To buflen - 1
       
        Print #2, Hex(buffer[i], 2); " ";
       
    Next i
   
Close #1
Close #2
It reads the entire contents of the input file into a string, then takes the characters in the string, one by one, and prints a hex code for each one into the output file, separating them by a space.
deadrats
Posts: 7
Joined: Jul 12, 2008 23:47

Postby deadrats » Jul 13, 2008 2:15

@counting_pine

hey thanks a bunch, compared to all the code that's required to perform a hex dump with C FB is a God sent. just a couple of things:

i tried to compile the code you posted (i'm using the FBIde development enviroment <---could that be the problem, it's just a front end for FB) and get the following errors:

Command executed:
"C:\FreeBasic\fbc.exe" "C:\free basic hex dump.bas"

Compiler output:
C:\free basic hex dump.bas(12) : error 10: Expected '=', found: 'As'
For i As Integer = 0 To buflen - 1
^

Results:
Compilation failed

System:
FBIde: 0.4.6
fbc: FreeBASIC Compiler - Version 0.15 for win32 (target:win32)
OS: Windows Server 2003 (build 3790, Service Pack 1)

while i'm picking your brain: is it possible to reverse the hexdump, in other words given a text file with a hex representation of the 1's and 0's that make up a file is it possible to rebuild the original file?

say something along these lines:

Dim buffer As String, buflen As Integer

Open "sample_2.file" For Binary As #1
Open "output.txt" For Input As #2

buflen = Lof(1)
buffer = Space(buflen)

Put #1, 1, buffer


For i As Integer = 0 To buflen - 1

Write #1, Hex(buffer[i], 2); " ";

Next i

Close #1
Close #2

once again thanks for your help and i must say i am really impressed with FB.
maddogg6
Posts: 824
Joined: Dec 07, 2005 22:58
Contact:

Postby maddogg6 » Jul 13, 2008 2:43

try:
For i As Integer = 0 To (buflen - 1)

I think it is from forgetting the parenthesis.

Or maybe

dim as integer beer = buflen - 1
For i As Integer = 0 To beer

or..should work no matter what....

dim as integer i, beer
beer = buflen - 1
for i = 0 to beer

Maybe as in for expressions doesn't like using vars/expressions like that?? a bug maybe?? naaa - that would have been caught.. ?? Nothing much about this on the for or for...next pages in the wiki.

Unless maybe your using a dialect or version of FB that doesnt support 'as' in 'for' expressions.. ??
Sisophon2001
Posts: 1704
Joined: May 27, 2005 6:34
Location: Cambodia, Thailand, Lao, Ireland etc.
Contact:

Postby Sisophon2001 » Jul 13, 2008 2:46

FBide has not been updated in a while, and it has a very old version of the compiler. You need to download and install the latest version.

Reversing the hex dump to get a binary file will work, but I would have to experiment a bit to get it right. Maybe later today, if somebody else does not do it first.

Garvan

PS.
This snipit works, but you should check to see if the binary file exists first and if it does delete the old file before writing out the new file. This is because binary access does not create a new file, but overwrites the old, and if the old file was larger that the new, then the output would not be correct. I used text files for testing, but output2.txt would be a binary file in real usage.

Code: Select all

Dim as string ch, src
dim as integer i


Open "output.txt" For Input As #1
Open "output2.txt" For Binary As #2


   while not eof(1)
      ch = input(1, 1)
      ch = ucase(ch)
      if (ch >= "0" and ch <= "9") or (ch >= "A" and ch <= "F") then
         src += ch
         i+=1
         if i = 2 then
            src = "&h" + src
            put #2, , cast(ubyte,val(src))
            i = 0
            src = ""
         end if
      end if

   wend

Close #1
Close #2
deadrats
Posts: 7
Joined: Jul 12, 2008 23:47

Postby deadrats » Jul 13, 2008 4:03

Sisophon2001 wrote:FBide has not been updated in a while, and it has a very old version of the compiler. You need to download and install the latest version.

Reversing the hex dump to get a binary file will work, but I would have to experiment a bit to get it right. Maybe later today, if somebody else does not do it first.

Garvan


you're right, i just downloaded the latest version of FB and just compiled the program from the command line and it works like a charm!!! just to be sure i used a hex editor to compare the output from the FB program and a known correct dump and in fact they are a spot on match, thanks to the guy that posted that code. (i am in shock at how easy it is to do a hex dump with FB, you should see all the code that's required to do it with C.)

incidently i noticed something odd that i'm hoping someone can explain to me: as i assume we all know, when data is stored on a hdd it's stored as a series of electrical states, either on or off and we think of this in human terms as 1's and 0's. each 1 or 0 is a bit and a byte is 8 bits. when we do a hex dump of a file (or when we use a hex editor to modify the bytes) we use an alphanumeric representation of said bytes but said alphanumeric representations are themselves stored as electrical states.

if we have a file, any file of size say 283KB and we do a hex dump we expect the resulting text file with the hex values to be bigger than the original file and in fact that is the case, as both the FB and C code give us the exact same output and resulting text file of the exact same size.

here's where it gets interesting, if we modify the C code so that instead of a hex dump it does a binary dump (i.e. it creates a text file of 1's and 0's ) the resulting file is bigger than the hex dump which is as expected. with FB a rather odd thing happens, i modified the code so that the program does a binary dump instead of a hex dump (basically i changed:

Print #2, Hex(buffer[i], 2); " ";

to

Print #2, Bin(buffer[i], 2); " ";

and it gives me a text file of 1's and 0's as expected BUT this text file is the exact same size as the text file with the hex dump!!! this shouldn't be, it should be at least 4 times bigger.

anyone have any idea what's going on (i am using the latest version of FB from the command line).

all help is appreciated.
deadrats
Posts: 7
Joined: Jul 12, 2008 23:47

Postby deadrats » Jul 13, 2008 4:12

Sisophon2001 wrote:This snipit works...


you are the man!!!! i just tested it out and it works like a charm, amazing, absolutely amazing, i can't believe how powerful and easy free basic is, from now on i'm only going to code in FB when i can.

incidently, anyone know of a nice FB IDE, maybe something like Lazuras for free pascal?
Sisophon2001
Posts: 1704
Joined: May 27, 2005 6:34
Location: Cambodia, Thailand, Lao, Ireland etc.
Contact:

Postby Sisophon2001 » Jul 13, 2008 4:34

Look at this code you posted,

Print #2, Bin(buffer[i], 2); " ";

You are printing out only 2 digits of the binary number, you need to change this to print out 8 digits.

I think FBEdit is the best Code Editor / IDE for Windows, and Geany for Linux. Others may have their own favourites.

Garvan
deadrats
Posts: 7
Joined: Jul 12, 2008 23:47

Postby deadrats » Jul 13, 2008 15:42

Sisophon2001 wrote:Look at this code you posted,

Print #2, Bin(buffer[i], 2); " ";

You are printing out only 2 digits of the binary number, you need to change this to print out 8 digits.

I think FBEdit is the best Code Editor / IDE for Windows, and Geany for Linux. Others may have their own favourites.

Garvan


well duh, of course if it's only printing out 2 digits i'm not getting a complete binary dump (<---i feel a bad joke coming on).

thanks for all your help (and the other guy that posted the dump code) and thanks for pointing me to FBEdit.

-'rats.

Return to “Beginners”

Who is online

Users browsing this forum: olympic sleeper and 7 guests