Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 15th, 2007, 12:45 PM
Authorized User
 
Join Date: Feb 2007
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to toddw607 Send a message via Yahoo to toddw607
Default Getting a set number of bytes from a file

Hi everyone,
    I have a seemingly simple problem which has been driving me crazy. I have a file consisting of a number of bytes and I want to take out eight bytes at a time, combine all eight bytes into a string, convert the new string to decimal and output the decimal to a text file. Here is my code that I've been working with:

Code:
                  Dim index As Integer = 8
                  For i = 0 To s1.Length() - 1 
                      bytes(i) = s1.ReadByte
                      If (i < index) Then
                          s2.WriteByte(bytes(i))
                      End If
                      index = index + 8
                  Next
      Here s1 is my filestream reader and s2 is my filestream writer. So far this prints out to the writer file the same thing as the reader file. I am not familiar with working with bytes and this has been driving me crazy. Can someone please help me out. Thanks!

 
Old August 15th, 2007, 01:07 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there... homework????

anyway.. what are s1 and s2?? files??? opened like what???

you are writing one byte at a time, the same one you just readed ;)

look at this:

Code:
    Dim index As Integer = 8
    For i = 0 To s1.Length() - 1 
        'you read from somewhere a byte, and you store it inside a position of an array
        bytes(i) = s1.ReadByte
        If (i < index) Then
            'you get the same byte you read before, and put in into another file
            s2.WriteByte(bytes(i))
        End If
        index = index + 8
    Next
I think your logic is somehow wrong.. you have to read 8 bytes from the source, combine them (what's the diference between file 1 and 2??) and then you have to save it to destination...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old August 15th, 2007, 01:17 PM
Authorized User
 
Join Date: Feb 2007
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to toddw607 Send a message via Yahoo to toddw607
Default

    This isn't homework, here's an explanation of what I'm trying to do: I'm trying to get coordinates of text out of a CGM file, convert the hex values into decimal and place the decimal coordinates into a transparent layer to fit over the CGM in order for the CGM to be clickable in certain spots.
    s1 (file 1) is defined as: s1 = New FileStream((FILE_NAME_IMAGE & fileName), FileMode.Open, FileAccess.Read). s2 (file 2) is defined as: s2 = New System.IO.FileStream(newName1, FileMode.Create, FileAccess.Write). All I am attempting to accomplish right now is reading in from s1, placing the hex values into a string (without any spaces between hex values) and place the string values into a text file. I know how to convert to a decimal after I get the string of 8 hex values into the file but all I am trying to do is get the string for every series of 8 hex values within the file. Sorry, I should have explained this before.

 
Old August 15th, 2007, 02:22 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

I understand what are you trying to do, but I don't understand the way you are trying to achieve it..

Code:
    ' the next for is not necessary, search a better method to know when you reach eof
    For i = 0 To s1.Length() - 1 
        for j as int16 = 0 to 7
            'the next line could throw and error if reach the oef.
            bytes(j) = s1.ReadByte
        next j
        'better replace last for with something like:
        's1.read(bytes,0,8) that does the same and return the number of
        ' bytes readed...
        'convert your byte array to what you need, don't know how.
        s2.WriteByte(yournewbyte)
    'the next line surely will be replaced if you change the way you read the file
    Next


HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
convert 4 bytes to bit array Vinny_68 Pro Visual Basic 2005 1 March 21st, 2007 05:40 PM
Script to delete files after a set number jkutey Beginning VB 6 2 October 29th, 2005 09:45 AM
How to set a user-defined Serial Number ? justin_min VS.NET 2002/2003 1 February 17th, 2005 07:48 AM
[?]how to set page number when printing? jodgen VB How-To 0 April 14th, 2004 02:30 AM
Bytes doa-wnloaded vincentvdm Classic ASP Professional 0 August 17th, 2003 01:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.