473,491 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reading bytes from a file

41 New Member
Hello,
I am reading binary in from a graphic file and storing it in a Byte Array and rewriting the binary data to a text file. Give that the bytes look something like:
ff ff f3 34 00 00 a3 f4
00 00 d3 a2 ff ff f2 ad
I am trying to capture four bytes at a time, place them into a variable and convert them into a readable value in Decimal using h = Val("&H" & someVariable). I then want to store these values into a separate text file to use to construct a transparent layer over the graphic (the above hex values are coordinates). Could anyone give me any ideas on how to start this project? Thank you so much.
Aug 13 '07 #1
5 1337
Plater
7,872 Recognized Expert Expert
You could just read 4 bytes at a time from the file stream?

Or keep an index varriable to keep track of where you are and use Array.Copy to copy parts of you main byte[] into a byte[4]

Have you looked into what the Bitmap/Image objects can do? They handle a lot of that kinda thing
Aug 13 '07 #2
toddw607
41 New Member
You could just read 4 bytes at a time from the file stream?

Or keep an index varriable to keep track of where you are and use Array.Copy to copy parts of you main byte[] into a byte[4]

Have you looked into what the Bitmap/Image objects can do? They handle a lot of that kinda thing

Hi Plater,
Thanks for the RE: The only problem with doing those two steps is the amount of loops I have to be able to get exactly what I want from the initial Hex file. The following is how the program stores the read-in values:
Expand|Select|Wrap|Line Numbers
  1.        '---Loop to get Coords out of intial Byte Array and place into secondary Byte Array
  2.                   For k = 0 To count                                   'Loops through entire binary CGM Byte Array
  3.                       If (bytes(k) = 164) Then
  4.                           If (bytes(k - 4) = 34) Then
  5.                               If (bytes(k - 6) = 0) Then
  6.                                   For z = -19 To -12
  7.                                       'Finds the positions where the Coords are located
  8.                                       bv.Write(bytes(k + z))    'Writes the bytes in the positions to the bv file
  9.                                       'Increments counter for next loop to keep counters seperated
  10.                                       yCoor(coorCount) = (bytes(k + z))
  11.                                       'writes Coords into yCoor Byte Array
  12.                                   Next
  13.  
  14.                               Else
  15.                                   For z = -17 To -10
  16.                                       bv.Write(bytes(k + z))
  17.                                   Next
  18.                               End If
  19.                           End If
  20.                       End If
  21.                   Next
  22.  
I'm not sure how I can read in at 4 bytes at a time using this logic and because I'm running through all the files within the folder one after another when I try to use a counter for this, the number is completely off, much too high. Any ideas on how I could get around this?
Aug 14 '07 #3
toddw607
41 New Member
You could just read 4 bytes at a time from the file stream?

Or keep an index varriable to keep track of where you are and use Array.Copy to copy parts of you main byte[] into a byte[4]

Have you looked into what the Bitmap/Image objects can do? They handle a lot of that kinda thing
Hi again Plater,

I tried to read in the file I wanted into that file but it wouldn't work. I looked up the reasoning why it wouldn't work and it seems that you cannot create a binary file then read it on the fly within the same program so I created a new one to read the file after all the files within the folder have been created. So my question is now, how to read the 8 bytes at a time inside the reading function. I've done this before with readers but it seems that the binaryReader works very differently.
Overall what I want to do is read in 8 bytes at a time, convert those eight bytes to decimal, then output each decimal number to a text file on it's own line. I'm sure I know how to do all the rest but reading in 8 bytes with the Binary reader is giving me more problems than it should. I'm sure this is an easy fix but I'm just not getting it.
Aug 14 '07 #4
Plater
7,872 Recognized Expert Expert
You could use a regular filestream's read method and read in byte[] at 8 bytes a time.
Then use BitConverter to change to a decimal and output to your text file.
Aug 14 '07 #5
toddw607
41 New Member
You could use a regular filestream's read method and read in byte[] at 8 bytes a time.
Then use BitConverter to change to a decimal and output to your text file.
I've been trying to do what you said but a regular filestream reader will not accept the byte property which is what my file consists of. I am using the following code:
Expand|Select|Wrap|Line Numbers
  1.  For i = 0 To br.BaseStream.Length() - 1
  2.                       nBytesRead = br.Read(ByteArray, 0, nBytes)
  3.                       bw.Write(nBytesRead)
  4.                   Next 
When I run this code, it places an 8 (which is what nBytes is assigned to) into a binary file, however it places a single 8 in the new binary file for every 8 bytes in the original file. For example, if there were 104 bytes in the original file then the new file will contain 13 number 8's, so I know it's working someway but I can't seen to obtain the correct data type to address it. Any thoughts?
Aug 14 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
2398
by: Ken | last post by:
Hello, I'm trying to read a Blob from Oracle and then write it to an audio file(.AU). I'm using Visual Studio.Net 2003 (VB). I can't seem to get my code to work. Will someone take a look at it...
20
3011
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
6
3742
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
40
4457
by: googler | last post by:
I'm trying to read from an input text file and print it out. I can do this by reading each character, but I want to implement it in a more efficient way. So I thought my program should read one...
3
9492
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
9
9835
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
11
3556
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
16
3716
by: Jm.GlezdeRueda | last post by:
Hi all, Im trying to read a 24bit bmp with fread, and i have some problems.. I want to read the whole structure in one time, but i dont know why, it only reads the first member well.. I have...
2
1235
by: Gerry | last post by:
Python 2.5, Windows XP. I have a 48-line text file written by a Windows python script, I try to read it as follows: f = open ("depstats.txt", "r", 0) for index, line in...
6
4211
by: jcasique.torres | last post by:
Hi everyboy. I trying to create a C promang in an AIX System to read JPG files but when it read just the first 4 bytes when it found a DLE character (^P) doesn't read anymore. I using fread...
0
7112
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6852
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.