reading binary files in array
reading binary files in array
reading binary files in array
reading binary files in array
reading binary files in array
reading binary files in array reading binary files in array reading binary files in array reading binary files in array reading binary files in array reading binary files in array reading binary files in array reading binary files in array
reading binary files in array reading binary files in array
reading binary files in array
Go Back  Xtreme Visual Basic Talk > > > reading binary files in array


Reply
 
Thread Tools Display Modes
  #1  
Old 04-08-2014, 04:45 PM
haider haider is offline
Newcomer
 
Join Date: Apr 2014
Posts: 24
Unhappy reading binary files in array


hi every one
i want to make a program in visual baisc 2010 to read any file time
avi mp3 mp4 mkv jpg zip rar txt pdf exe dll and put its binary data in an array
or storage it in RAM

i tried to write a code and use filesteam but i fail

HTML Code:
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim myCoolFile As String = "C:\AMD\1.txt"



        Dim myFileStream As System.IO.FileStream
        myFileStream = New System.IO.FileStream(myCoolFile, _
                 IO.FileMode.OpenOrCreate, IO.FileAccess.Read, IO.FileShare.ReadWrite)




        Dim x As Integer = myFileStream.Length - 1
        Dim myArray(x) As Byte
        Dim i As Integer = 0
        For Each mybyte As Byte In myFileStream
            myArray(i) = mybyte
            i += 1 '
        Next

        TextBox1.Text = x



    End Sub
End Class
Reply With Quote
  #2  
Old 04-08-2014, 04:47 PM
haider haider is offline
Newcomer
 
Join Date: Apr 2014
Posts: 24
Default

i want to make a program like this one

http://www.kahusecurity.com/2012/binary-file-converter/
Reply With Quote
  #3  
Old 04-09-2014, 03:33 AM
PlausiblyDamp's Avatar
PlausiblyDampreading binary files in array PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

The documentation for a FileStream is at http://msdn.microsoft.com/en-us/libr...ilestream.aspx and I can't see anywhere in the documentation that says a filestream can be used in a For ... Each Loop like you are attempting to do.

If you want to read all bytes then there is a .Read method (http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx) that will fill an array. If you are on a later version of .Net then you might want to consider the Async version of the Read method (http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx)
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #4  
Old 04-09-2014, 10:16 AM
passel's Avatar
passelreading binary files in array passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

If I wanted to read all the bytes from a file, I think I would use the ReadAllBytes method of the File class, rather than use a FileStream.
File.ReadAllBytes documentation.
Your code updated...
Code:
Public Class Form1
    Dim myArray() as Byte

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim myCoolFile As String = "C:\AMD\1.txt"

        myArray = IO.File.ReadAllBytes(myCoolFile)

        TextBox1.Text = myArray.Length - 1

    End Sub
End Class
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #5  
Old 04-09-2014, 10:45 AM
haider haider is offline
Newcomer
 
Join Date: Apr 2014
Posts: 24
Default

thank you very much


but if i want to read any sector of the array i didn't see it in hex

HTML Code:
Public Class Form1
    Dim myArray() As Byte

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim myCoolFile As String = "C:\AMD\1.txt"

        myArray = IO.File.ReadAllBytes(myCoolFile)
        TextBox1.Text = myArray(5)

    End Sub
End Class
and also if i want to read photo there will be error when i compile the code

HTML Code:
Public Class Form1
    Dim myArray() As Byte

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim myCoolFile As Byte = "a.png"

        myArray = IO.File.ReadAllBytes(myCoolFile)
        TextBox1.Text = myArray(5)

    End Sub
End Class
Reply With Quote
  #6  
Old 04-09-2014, 06:06 PM
PlausiblyDamp's Avatar
PlausiblyDampreading binary files in array PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

The code
Code:
Dim myCoolFile As String = "C:\AMD\1.txt"

myArray = IO.File.ReadAllBytes(myCoolFile)
TextBox1.Text = myArray(5)
will do as you asked and read the raw bytes of a file into a byte array.

Not sure what you mean by
Quote:
but if i want to read any sector of the array i didn't see it in hex
but you can access the bytes of the array individually or either loop over a range or use Array.Copy (http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx) to extract a chunk of the array - is that what you mean?

When you referred to not seeing it in hex, where are you looking? Bytes are just bytes, there aren't special hex-bytes. By default the debug windows in visual studio display in decimal but this can be changed. If you want to display an individual byte in hex then you can always use .ToString("X") on the byte....

If you are have set Option Strict On (which you should have done) then
Code:
TextBox1.Text = myArray(5)
will throw an error as you will need to convert the byte to a string, try
Code:
TextBox1.Text = myArray(5).ToString("X")
Code:
Dim myCoolFile As Byte = "a.png"
will certainly error as you are trying to declare a variable as a byte and set it to a string, this has nothing to do with loading a jpeg or a png. If you use the same code but a filename that is a png you will load the raw bytes of the png.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #7  
Old 04-10-2014, 04:10 AM
haider haider is offline
Newcomer
 
Join Date: Apr 2014
Posts: 24
Default

thank you sir for helping me
finally i could write this

HTML Code:
Imports System.IO
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            Label1.Text = "File Size : "
            TextBox1.Text = ""
            Dim b As String
            Dim i As Long
            Dim fInfo As New FileInfo(OpenFileDialog1.FileName)

            Dim numBytes As Long = fInfo.Length
            Dim a As Integer

            Dim fStream As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)

            Dim br As New BinaryReader(fStream)

            Dim data As Byte() = br.ReadBytes(CInt(numBytes))
            a = numBytes
            a = a / 1024


            ' Label1.Text = Convert.ToString(data(1)) 
            Label1.Text = Label1.Text & a & " KB"
            For i = 0 To (fInfo.Length - 1)
                b = b & data(i)
            Next
            TextBox1.Text = TextBox1.Text & b

            br.Close()

            fStream.Close()
        End If
    End Sub

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class

and this is the source code for my program
download it and compile it
http://www.gulfup.com/?0eq1jO

but sir the value is not in hex as this program
www.kahusecurity.com/2012/binary-file-converter/


and can you optimize the code to make it more faster and better
by write it in another way or use another function

i want it to open large file fast


my goal is to make the program show the byte in hex
i want it to store every bit in array elements (but i thing this is impossible in visual basic 2010 )

thank you again for helping me sir

MY REGARDS
Reply With Quote
  #8  
Old 04-10-2014, 04:14 AM
haider haider is offline
Newcomer
 
Join Date: Apr 2014
Posts: 24
Default

i search and find this function

http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx
i will try to use it in the program
Reply With Quote
  #9  
Old 04-10-2014, 07:00 AM
haider haider is offline
Newcomer
 
Join Date: Apr 2014
Posts: 24
Default

hi sir

i could use the hex function in vb2010 to convert to hex

HTML Code:
Imports System.IO
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            Label1.Text = "File Size : "
            TextBox1.Text = ""
            Dim b As String
            Dim i As Long
            Dim fInfo As New FileInfo(OpenFileDialog1.FileName)

            Dim numBytes As Long = fInfo.Length
            Dim a As Integer

            Dim fStream As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)

            Dim br As New BinaryReader(fStream)

            Dim data As Byte() = br.ReadBytes(CInt(numBytes))
            a = numBytes
            a = a / 1024

            ' Show the number of bytes in the array.
            ' Label1.Text = Convert.ToString(data(1))
            Label1.Text = Label1.Text & a & " KB"
            For i = 0 To (fInfo.Length - 1)
                b = b & Hex(data(i))
            Next
            TextBox1.Text = TextBox1.Text & b

            br.Close()

            fStream.Close()
        End If
    End Sub

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class

and this is the source code
http://www.gulfup.com/?xSPGan


but there is a problem
when the program convert the binary code to hex
the digit which contain double zero "00"

http://im76.gulfup.com/5bpNrg.jpg

didn't convert to double zero in hex

as like this program
http://www.kahusecurity.com/2012/binary-file-converter/

see this photo

http://im76.gulfup.com/xAXXWk.jpg


in my program it convert to single digit
http://im76.gulfup.com/sdUKB3.jpg

i make this file if you want it to test
http://www.gulfup.com/?jsCeKG

can you optimize the code
is there any wrong in the code




thank you very much
Reply With Quote
  #10  
Old 04-10-2014, 11:19 PM
passel's Avatar
passelreading binary files in array passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

How large are the files you're dealing with? A textbox might have a problem with really large file, although I haven't tested it in VB.Net.

Perhaps not the fastest, but fairly simple off the top of my head example and should be much faster than what you've got so far.
Code:
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    TextBox1.Font = New Font("Courier New", 9) 'Fixed space font, so lines line up
  End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim b() As Byte = IO.File.ReadAllBytes("c:\test.bmp")
    Dim sb As New System.Text.StringBuilder(b.Count * 2 + b.Count \ 8) 'preallocate size of buffer 2 chars per byte + 2chars per line of 16 bytes
    For i As Integer = 1 To b.Count
      sb.Append(b(i - 1).ToString("X2")) 'append byte as 2 hex digits to string builder
      If i Mod 16 = 0 Then sb.Append(vbNewLine) ' add newline to do 16 bytes per line
    Next
    TextBox1.Text = sb.ToString 'convert string builder buffer to a string 
  End Sub
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 04-10-2014 at 11:38 PM.
Reply With Quote
  #11  
Old 04-11-2014, 03:38 AM
haider haider is offline
Newcomer
 
Join Date: Apr 2014
Posts: 24
Unhappy

Mr.passel thank you very much
it work very well thank again for helping me

also thank you Mr.PlausiblyDamp for helping me

know i have general question

about Visual Basic .net (2010 or 2013 or the new version in the future)

i have many books downloaded torrents or other sources about this language
but i didn't find a clear book to learn this language

all the book start from advance they didn't tech you how to write the code step by step

and make you understand the structure of the code

as example when i want to write a program in vb 2010 and double click on button or textbox to write the code
when i start to write first character there will be a menu for many function and instruction
why there is no book or website have the description for all these functions
also when i go to object browser there will be form have all the vb instructions and function (as i thought) may be this is wrong idea

so how to understand all of there

i want to write the code by myself
the programmer in not the person who write the program

no the programmer is the person who can solve the problem in programming language

and today i have problem in binary file reading
and i didn't solve it by myself
i don't want to be that the person who ask people for every thing i want to learn an make what i want by myself
I'm in the third year in college and i have 22 years know I'm not professional in any programming language
just know little about c , c++ and visual basic 6 and visual basic 2010

can you Mr.passel and Mr.PlausiblyDamp

tell me from where to start and what book recommend to me

that all what i need

thank you very much

Last edited by haider; 04-11-2014 at 04:02 AM.
Reply With Quote
  #12  
Old 04-12-2014, 10:30 AM
haider haider is offline
Newcomer
 
Join Date: Apr 2014
Posts: 24
Default

i will waiting
Reply With Quote
  #13  
Old 04-14-2014, 12:27 PM
passel's Avatar
passelreading binary files in array passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

I didn't reply before, because it is not a simple thing to answer.
I have a few VB.Net books, bought when I first started trying to get familiar with VB.Net in 2007 or there abouts, so were .Net 2005 related books, but I can't really quantify how useful they were to my learning VB.Net.
I have a long history with computers of all types, and a number of languages, but for hobby purposes, it was primarily the various BASIC interpreters and later compilers, mostly Microsoft based. None of my work related programming involved BASIC or Visual Basic, except in those rare cases where some little tool was needed, and I could slap together something quickly to meet the need, using my own licensed copy of whatever current QB compiler or Visual Basic that was current.
Only recently, has my current department bought copies of Visual Studio .Net for each programmer in the department, although not regularly, so we had Visual Studio 2005 for a few years, and currently have licensed version of VS 2010 for each engineer, so VB 2010 is what I "work" with, when I can (which is quite often as of late).

Anyway, because of the history, I don't really know how much I learned from a book, and how much from forums, and searching the net for answers. Percentage wise, I'm sure the majority of learning came from searching the net.
When you're in the IDE and you see the list of methods from intellisense, you can always select one to put in the code and then put the cursor on it, and hit the F1 key to get information about the method, if you have an internet connection, or MSDN installed.
Learning VB.Net is really two (at least) major areas of study. One is the language itself, which is the easier part, in my opinion, and second is the .Net framework, which is huge and monstrous, and a very large part of what you have to deal with, regardless of which .Net language you choose, i.e. VB.net, C#, F# etc..

So, I don't have any real recommendations, but just for some response, did a quick search on "definitive vb.net tutorial" to see what hits may come up.
Several of the first hits had to do with an older VB.Net book, "The Visual Basic .NET Programming Language" by Paul Vick.
I have access to Safari Books online, so took a quick look at the book. The one I looked at was published in 2004, and I don't know if there has been an update, but from a quick perusal of the contents, it does seem to focus on the VB.Net language itself, so doesn't try to cover the .Net framework in any detail, so won't have sections on graphical programming, File I/O, or databases etc.
So, if some other books seem too complex, this book might suit your purpose as a beginning point in VB.Net, since the emphasis is on the language, not the .Net framework.

On the negative side, though, this book would not have answered your question or lead you to it, as the solution to your problem depends on classes provided by the .Net framework. But, maybe as a first step, this book would get you past the initial hurdle of understanding how the language is organized, and then you can continue with the books that start pulling the .Net framework into the picture.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #14  
Old 04-14-2014, 02:29 PM
haider haider is offline
Newcomer
 
Join Date: Apr 2014
Posts: 24
Default

thank you for answer Mr.Passel

but every time i open Visual Studio i didn't know what should i do
there is C++ Visual Basic and C#

this is very big question to asked and i search in Google and i found many answers
about the difference and every language for any purpose it used

i thing in the last i'll use Visual C++ in stead of using Visual Basic or C#
because it's better and many programmers prefer it in programming programs that depends on the speed of execution the instruction

i want to linked between embedded system like arduino or FPGA device and Computer
so i thing C++ will be the Best choose to to that

and if you have and opinion please tell me i will be happy to hear from you Sir

Thank you Very much

My Regards
Reply With Quote
  #15  
Old 04-15-2014, 12:12 PM
Cerian Knight's Avatar
Cerian Knightreading binary files in array Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,199
Default

Here is a historical article that might help in decision making:
http://msdn.microsoft.com/en-us/magazine/cc301505.aspx
... however, with .NET languages, there is less of an issue with speed of execution because identical programs written in each language, if I recall correctly, can compile to the exact same (or similarly performing) code.
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
  #16  
Old 04-15-2014, 01:29 PM
PlausiblyDamp's Avatar
PlausiblyDampreading binary files in array PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Better is a very subjective term when dealing with programming languages, you really need to decide what is important in a language for you / your current project and choose based on those criteria.

C++ has many powerful features, however it can still involve very complex and difficult to understand syntax and language rules.

.Net, especially later versions, provide very powerful syntax through features like Linq while allowing much easier ways of dealing with multithreaded or asynchronous scenarios for starters.

What kind of software are you going to be developing?
Reply With Quote
  #17  
Old 04-15-2014, 04:17 PM
haider haider is offline
Newcomer
 
Join Date: Apr 2014
Posts: 24
Default

software to connect between embedded system hardware and windows OS
with GUI to control the Embedded System or show the data
may be need to use Nvidia Cuda or other multicore programming technique for better performance
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
reading binary files in array
reading binary files in array
reading binary files in array reading binary files in array
reading binary files in array
reading binary files in array
reading binary files in array reading binary files in array reading binary files in array reading binary files in array reading binary files in array reading binary files in array reading binary files in array
reading binary files in array
reading binary files in array
 
reading binary files in array
reading binary files in array
 
-->