Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2364
  • Last Modified:

Write Binary/Map File?

I need to write to a binary/map file. Is this even possible with Perl?
0
Greggory
Asked:
Greggory
  • 3
  • 2
1 Solution
 
ozoCommented:
What leads you to think it might not be?
On some operating systems you might have to set
binmode()
0
 
GreggoryAuthor Commented:
I didn't think it was possible because I can't get it to work.

#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);


print "Content-type: text/html\n\n";

open(OUT, ">>test.map") or die("Couldn't open output file: $!");
binmode(OUT);
print OUT "00000000 0D FE FF 23 94 FE FF FF FF FF FF FF FF FF FF FF";
close(OUT);

I want a binary file, but when I do ^ that, and open it in Visual Studio, it opens as a text file, not a binary file. Am I doing something wrong, or is it not possible to write text to a binary file?
0
 
GreggoryAuthor Commented:
I little Googling found layers. I tried raw, but that didn't do anything. I found bytes and that does something. If I start with a binary file and write to it, I still get a binary file (unlike raw), but I run into the problem of what do I output? If I want "00000000 0D FE FF 23 94 FE FF FF FF FF FF FF FF FF FF FF" and I output that, I get:
30 30 30 30 30 30 30 30 20 30 44 20..... and so on. Would I just output something like "Hello" and it would convert everything for me?
0
 
GreggoryAuthor Commented:
Another problem, this only seems to work if I append to the file. If I try to over write the file, it switches back to text.
0
 
ozoCommented:
Quotes enclose text.  "FF" is a string containg two "F" characters.  If you wanted a byte with value 255,
you'd have to convert from a string in hexadecimal notation into packed bytes

open(OUT, ">>test.map") or die("Couldn't open output file: $!");
binmode(OUT);
print OUT pack"H*",join'',split" ","00000000 0D FE FF 23 94 FE FF FF FF FF FF FF FF FF FF FF";
close(OUT);

(the join'',split" ", removes the spaces before packing the hex string)
You could also directly print the byte values

print OUT "\c@\c@\c@\c@\cMþÿ#þÿÿÿÿÿÿÿÿÿÿ";



0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now