-
March 28th, 2009, 07:43 PM
#1
Compressing a File by its Binary
Hi, sorta new here so hope its cool I ask this, I search the forum, but I may have missed if someone has done this befor.
I'm still new to VB coding and i'm still learning it from my high school class I am taking. I do programing with other langs so i'm not totally new to programing it self. Also i'm using VB5 at my house but have VB6 at school.
My question is, can VB read and write binary data files?
If so, how would you go about reading its binary data I tried with the code below but not sure if i'm doing it right, cuase when it displays the data it shows it as a Dec. not its Bin. From there I want to take its binary data and try and find a mathmatical expression that, when work out, will be the same with the 1s & 0s.
Code:
Private Sub Form_Load()
Dim srcFile As String
Dim destFile As String
Dim B() As Byte
Dim FileLength As Long
srcFile = "C:\Documents and Settings\Owner\Desktop\hi.txt"
destFile = "C:\Documents and Settings\Owner\Desktop\hi1.txt"
Open srcFile For Binary Access Read As #1
FileLength = FileLen(srcFile)
ReDim B(1 To FileLength)
Get #1, , B
For i = 1 To FileLength
txt = txt & B(i) & vbCrLf
Next i
Close #1
Open destFile For Binary Access Write As #1
Text1.Text = txt
Close #1
End Sub
-
March 28th, 2009, 07:52 PM
#2
Re: Compressing a File by its Binary
Nope. That's how to read a TEXT file... Line by line.
This is BINARY. You read it all at once. Then, I used an API to copy the contents to a string and used it as the function result
Code:
Option Explicit
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Public Function ReadFile(Filename As String) As String
Dim FNum As Integer
Dim Contents() As Byte
Dim sStr As String
Dim Length As Long
FNum = FreeFile
On Local Error GoTo ErrorRtn
Open Filename For Binary As #FNum
If LOF(FNum) = 0 Then
Close #FNum
Exit Function
End If
ReDim Contents(LOF(FNum))
Get #FNum, , Contents(): Close #FNum
Length = UBound(Contents)
sStr = String(Length, " ")
CopyMemory ByVal sStr, Contents(0), Length
ReadFile = sStr
ExitRtn:
Exit Function
ErrorRtn:
MsgBox Err.Number & " - " & Err.Description, vbCritical, App.Title
Resume ExitRtn
End Function
-
March 28th, 2009, 11:28 PM
#3
Re: Compressing a File by its Binary
I got your code working, but feels the same as the code I used befor hand. SO how would you go about seeing the binary data, I have a feeling the computer automagicly converting the binary data back into ascii keys?
Just for example.
I have a text file with the number 3 in it. I know by converting the 3 you would get the binary code of 0011. I get the 3 with your code and with the other code I get its Bin instead of its binary.
Last edited by Mradr; March 28th, 2009 at 11:31 PM.
-
March 29th, 2009, 12:59 AM
#4
Re: Compressing a File by its Binary
When you read a binary file or any file in binary mode you get the bytes not the bits 0s and 1s [bits] make up the bytes but they are read as bytes you would have to do a conversion on this data to see the actual string of 0s and 1s
-
March 29th, 2009, 11:49 AM
#5
Re: Compressing a File by its Binary
If it were text, you could convert each character one at a time, but if some other program created it, then you'd have to know the format that was used to write the data.
-
March 29th, 2009, 12:46 PM
#6
Re: Compressing a File by its Binary
Originally Posted by dglienna
If it were text, you could convert each character one at a time, but if some other program created it, then you'd have to know the format that was used to write the data.
Oh I know that, but I want to make a more powerfull compression software that can in a way compress all softs of data by its binary, if I can read the bits the 1s and 0s, then I can convert that to a higher base and then use a mathmatical expression to get the ones and zeros again.
So DataMiser how would you go about converting the bytes to bits?
-
March 29th, 2009, 01:01 PM
#7
Re: Compressing a File by its Binary
Not really sure. I know it can be done but never had a need to do it. Not sure how it would help you though as they are already written to the file in hex which means 256 possible bit patterns per byte.
I suppose it would be possible to expand on the hex base to use more letters than the conventional A-F. In theory you might be able to get a pretty good compression rate with such a method but I have not tried this either. Then again that is just a thought. It may not help at all.
Last edited by DataMiser; March 29th, 2009 at 01:14 PM.
-
March 29th, 2009, 01:17 PM
#8
Re: Compressing a File by its Binary
On second thougt I do not think that would work as you still would not be able to write a value outside the range of hex into a single byte.
-
March 29th, 2009, 01:49 PM
#9
Re: Compressing a File by its Binary
Originally Posted by DataMiser
On second thougt I do not think that would work as you still would not be able to write a value outside the range of hex into a single byte.
True, you cant, but I figure you could hit this 2 differnt ways.
1. Way is to find a math problem that just spit out 1s and 0s when givin a number or two so when work out it give the same order of 1s and 0s as the org. copy.
*So for example from 0110,1010,1011,1110,1010,1110,1010,1111,0111, to just the number 43 (0100,0011), do a little math 43^2*3 = the bits as befor*
2. Another is to convert Hex numbers into base 64 (1-9-abc-!-/) so any number higher then 15 and lower then 64 will get a size cut of one half. From there you can do another math problem like we did with binary, that will use base 64, and spit out the a number that when work out in our decompression code be the same order as the org. copy again.
Last edited by Mradr; March 29th, 2009 at 01:57 PM.
-
March 29th, 2009, 02:40 PM
#10
Re: Compressing a File by its Binary
-
March 30th, 2009, 12:24 PM
#11
Re: Compressing a File by its Binary
Here is something I threw together based on the examples in the link above. Basically I just modified the sample a little.
it expects to see the numeric value for the byte as would be returned by the ASC() function
Code:
Public Function ByteToBinary(ByVal byte_value As Byte) As String
Dim hex_string As String
Dim digit_num As Integer
Dim digit_value As Integer
Dim nibble_string As String
Dim result_string As String
Dim factor As Integer
Dim bit As Integer
' Convert into hex.
hex_string = Hex$(byte_value)
hex_string = Right$( "00" & hex_string, 2)
' Read the hexadecimal digits
' one at a time from right to left.
For digit_num = 2 To 1 Step -1
' Convert this hexadecimal digit into a
' binary nibble.
digit_value = CLng("&H" & Mid$(hex_string, _
digit_num, 1))
' Convert the value into bits.
factor = 1
nibble_string = ""
For bit = 3 To 0 Step -1
If digit_value And factor Then
nibble_string = "1" & nibble_string
Else
nibble_string = "0" & nibble_string
End If
factor = factor * 2
Next bit
' Add the nibble's string to the left of the
' result string.
result_string = nibble_string & result_string
Next digit_num
' Return the result.
ByteToBinary = result_string
End Function
Last edited by DataMiser; March 30th, 2009 at 12:27 PM.
-
March 30th, 2009, 01:42 PM
#12
Re: Compressing a File by its Binary
Open a RTF file, as either binary or text. Try to encrypt that...
-
March 30th, 2009, 02:54 PM
#13
Re: Compressing a File by its Binary
What would be the point?
It does not matter if the file is an rtf, jpg, bin, exe or whatever. The goal is to compress the file rather than encrypt it though it would result in a cryptic file. At any rate the decompressed file will be an exact match to the original no matter what type of file we started with. The only question is how much can it be compressed. I would think RTF could be compressed dramatically.
Last edited by DataMiser; March 30th, 2009 at 02:58 PM.
-
March 30th, 2009, 03:19 PM
#14
Re: Compressing a File by its Binary
Originally Posted by dglienna
Nope. That's how to read a TEXT file... Line by line.
This is BINARY. You read it all at once.
Just so our readers are aware there is no rule that you must read a binary file all at once. you can read 1 byte at a time if you like [slow but it does work]
You can read it in blocks of pretty much any size you wish. [larger is generally better]
You can read it into a structure defined using the type definition
You can read it a few bytes at a time into a INT a LONG, a DOUBLE, a String, an array or whatever.
With binary access you can read it pretty much anyway you want it is depending on the results you are looking for what would be the best way to go about it.
For example you may want to read 256 bytes from a file that is 1 gig in size and the bytes are located at the end of the file. No need to read the whole file when you can just seek to the position 256 from the end and read the 256 bytes you want.
If on the other hand you want to read a text file line by line you would not be advised to use binary access at all. Instead use the input option as the open type and use line input statements to get each line.
-
March 30th, 2009, 03:36 PM
#15
Re: Compressing a File by its Binary
The following piece of code
Code:
ReDim Contents(LOF(FNum))
Get #FNum, , Contents(): Close #FNum
Length = UBound(Contents)
sStr = String(Length, " ")
CopyMemory ByVal sStr, Contents(0), Length
ReadFile = sStr
could be written as
Code:
sStr = String(LOF(FNum), " ")
Get #FNum, , sStr: Close #FNum
ReadFile = sStr
Same result, no api calls needed and likely will run faster
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|