Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting Files

Status
Not open for further replies.

Courtney1306

Programmer
May 30, 2007
14
US
I am trying to convert a binary file into a CSV file in Perl. I have the binary file saved on my computer and I need to import that into Perl. I then want to convert it to a CSV and save under a new name to be used in a Vector Signal Processor. Does anyone have code to help me with this? I am new to this programming language and don't know where to start. Thanks!

Courtney
 
The basic format:

Proprietary binary data file composed of several header fields of unsigned binary data followed by a list of 2 byte signed data items.

I want to take the data items and convert them into a CSV file.
 
several header fields" is too vague to give you any definite answers to anything, I'm afraid.
 
There are 4 parameters and each has a different set unsigned bytes. They vary from 1 to 8, adding up to 18 total unsigned bytes.
 
This is what you would do in a generic way.
read, grab off bytes out of the read in line
and then use unpack to convert to ascii

Code:
$rc=open(INFILE, "<$bDir\\$File"); 
binmode(INFILE);
$rco=open(OUTFILE, ">$OFile"); 
## assume each binary line is 100 bytes
while(1)
	## Read in 1st 100 bytes 
	$rc=sysread(INFILE, $line,100);
        if (nomorelines)
                 { LAST };
        ## grab input data
        $byte = substr($line,1,2);
        ## see unpack
        $number = unpack("i", $byte);

	print OUTFILE ($number . "," . "\n") ;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top