ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Help reading binary file (https://www.excelbanter.com/excel-programming/305386-help-reading-binary-file.html)

Luis[_5_]

Help reading binary file
 
I would like to open the data contained in a binary file in to an
excel worksheet. I can open the binary file in python with the
following script

################################################## #######################
def rsvdat(filename):
header=array.array('c') # Skip header
memsize=array.array('L')
file=open(filename,'rb')
# Define header as 1032 bytes dont use it
header.fromfile(file,1032)
# Define buf size as 4 byte long int
memsize.fromfile(file,1)
# This will give the number of data points (4 bytes floats)
totpts=int(memsize[0]/4.0)
v=array.array('f')
v.fromfile(file,totpts)
file.close()
v=Numeric.array(v) #convert to Numeric type to use
slicing ops
vc=Numeric.zeros(totpts/2,'D')
vc.real=v[::2]
vc.imag=v[1::2]
return vc

Now I want to do it in VBA, so far I got
'================================================= =============
Sub rsvd()
'================================================= =============
Dim Rdata As String
Dim BinaryFile As String
Dim numPts As Long
Dim SVpoint As Single ' 4 bytes right ?
'================================================= =============


FileNum = FreeFile ' Supply next available number
BinaryFile = Application.GetOpenFilename
'Workbooks.Add
Set NewSheet = Sheets.Add(Type:=xlWorksheet)
'NewSheet.Name = BinaryFile 'THIS DOESN"T WORK
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
NewSheet.Select

Open BinaryFile For Binary Access Read As #FileNum
Seek #FileNum, 1032 ' Skip header
'numPts = Input(4, #FileNum) DONT LIKE IT, WANTS STRING
Input #FileNum, numPts "WORKS" BUT IT DOESNT RETURN RIGHT VALUE
Range("A1").Value = "Now the data :"
'================================================= =============
' Loop to get total number of points
For k = 1 To numPts
Input #FileNum, SVpoint
Range(Cells(k, 2), Cells(k, 2)).Value = otro
Next k
'================================================= =============
Close #FileNum
End Sub
'================================================= =============

at this point I am stuck !
If I cannot retrieve the total number of points (after 1032 bytes) i
cannot loop, furthermore
if I get the next value (should be a float) always return 0.

Am I using the Input # function correclty, or is there an alternative
to get data form the binary
file

TIA

Luis

keepITcool

Help reading binary file
 

workbooks.add
activesheet.name="BINARYFILE"
...

use FileLEN to get the length of file.


cant use INPUT on binary files, you'll need GET/PUT instead.
see VBA help on GET statement

sorry to be brief, hope it gets you on the way.

--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Luis wrote :

I would like to open the data contained in a binary file in to an
excel worksheet. I can open the binary file in python with the
following script

FileNum = FreeFile ' Supply next available number
BinaryFile = Application.GetOpenFilename
'Workbooks.Add
Set NewSheet = Sheets.Add(Type:=xlWorksheet)
'NewSheet.Name = BinaryFile 'THIS DOESN"T WORK
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
NewSheet.Select

SNIP


Am I using the Input # function correclty, or is there an alternative
to get data form the binary
file

TIA

Luis



Luis[_5_]

Help reading binary file
 
That was actually all I needed to know
its is on its way to work now!

"keepITcool" wrote in message ...
workbooks.add
activesheet.name="BINARYFILE"
..

use FileLEN to get the length of file.


cant use INPUT on binary files, you'll need GET/PUT instead.
see VBA help on GET statement

sorry to be brief, hope it gets you on the way.

--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Lu



All times are GMT +1. The time now is 05:11 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com