reading a binary file help

brodaddy2002

Board Regular
Joined
Jan 21, 2013
Messages
67
I have the following code below. I am trying to read DTED data into excel. I am not really sure what I am doing, but I think I am close. A couple of questions.

1) Can we use for input instead of Binary Access Read in the Open statement? I couldn't get for input to work.
2) Why do we not need a hash in front of intFileNum
3) What type variable is intFileNum?
4) Why isn't temp changing values? It when I try to do arithmetic?

Any help will be greatly appreciated


Code:
Public Sub reading3()
Dim intFileNum, bytTemp As Integer
Dim Letter As String
Dim i As Long
Dim j As Long
Dim test() As Integer
'Dim NewArray() As Integer
Dim x As Long
Dim temp As Integer




j = 1
intFileNum = FreeFile
Open "C:\Users\x\Documents\Thesis\Fallon DTED\rasexp_dted_1365124850_77725\dted\w114\n35.dt0" For Binary Access Read As intFileNum
'Seek intFileNum, 2
EData = filelen("C:\Users\x\Documents\Thesis\Fallon DTED\rasexp_dted_1365124850_77725\dted\w114\n35.dt0")
x = EData - 3428
ReDim NewArray(0 To EData) As Variant
For i = 3428 To EData
Seek intFileNum, i
Get intFileNum, , bytTemp
temp = bytTemp
If temp < 0 Then
    temp = -1 * ((temp + 32767) + 1)
End If


Debug.Print bytTemp


Range("A" & j) = bytTemp


j = j + 1
'    For j = 0 To (EData - 3428)
'        NewArray(j) = bytTemp
'    Next
'Letter = Chr(bytTemp)
Next
Debug.Print Letter
Close intFileNum
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
1. Please read the syntax in the developer's help for "Open" statement. Following line shall work:
Code:
Open "C:\Users\x\Documents\Thesis\Fallon DTED\rasexp_dted_1365124850_77725\dted\w114\n35.dt0" For Input Access Read As #intFileNum

2. # is optional. See here:
WD2000: Using the Open Statement in Word VBA
It coerces the variable to be of numeric type like $ forces a variable to become string.

3. intFileNum should be integer but as first three letters would suggest but it is variant in your case. The line
Code:
Dim intFileNum, bytTemp As Integer
should be:
Code:
Dim intFileNum As Integer, bytTemp As Integer

4. The code construct shall be like:
Code:
Public Sub ReadBinFile()
Dim intFileNum, bytTemp As Integer
Dim strData As String
intFileNum = FreeFile
Open "C:\Users\x\Documents\Thesis\Fallon DTED\rasexp_dted_1365124850_77725\dted\w114\n35.dt0" For Input Access Read As #intFileNum
Do While Not EOF(intFileNum)
    Line Input #intFileNum, strData
    'strData will hold your data line by line.
    'put your processing code here
Loop
Close #intFileNum
End Sub
 
Upvote 0
The file is not a text file. Does that change things? The first portion of the file is in bytes, and the last portion is in integers.
 
Upvote 0
I'm not sure as I have not come across such data format in the past. You can give it a try anyway and get back. Maybe then one of the board experts will be able to help you.
 
Upvote 0
I got it to read data, but couldn't get the Temp integer to update when bytTemp updated. I was trying to create an array with bytTemp, but lost. For some reason, I could export the data into an excel worksheet, and I think that I could create an array using ranges, but I was trying to be a little more elequent in my design.

The link to the data is below. I was tring to read the .dt0 files. The first 3427 bytes are ASCII bytes. The integers start at bytes 3428. I very much appreciate your assistance.

https://www.box.com/s/m8ppxqjifvcib50zjnx5
 
Upvote 0
The first 3427 bytes are ASCII bytes.
Where in the spec did you see that?

Also, the file should have 121 x 121 2-byte posts (29,282 bytes) + the header, which doesn't reconcile with the file size. I'm looking at W114 N35.
 
Upvote 0
That is where my knowledge gets limited. You are correct that it should produce a 121x121 array. I looked at the function DTED in Matlab, and they start importing at byte 3428 in Big-endian ordering. The data gets a little wierd with some of the integers are negative and some are in 2's complement. Like you said MIL-PRF-8902B, Section 3.11.1 warns us about this issue. I was trying to do some arithmetic on bytTemp, but couldn't get it to work. I will upload the DTED function code in box.com for reference.

https://www.box.com/s/99wki77en542kc4il12u
 
Upvote 0
Elevations below sea level (e.g., Death Valley) are negative, and negative values are sign/magnitude, not two's complement.

Page 30 shows the data record format, the first of which begins in byte 3429:

Hex AA marks the beginning of a record

7 more bytes described in the table

Then the data (2 bytes per post)

Then a 4-byte checksum

Then rinse and repeat

That agrees with the size of the file.

I'm using XVI32, a free hex editor to look at the file -- very convenient.
 
Last edited:
Upvote 0
Last edited:
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,202
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top