Hello. I'm trying to write a script that involves reading in data from one binary file, changing it, and outputting the result to a new binary file. To be specific, it reads in 2 bytes at a time from the first .bin file, does a bitwise AND with a specific value (0xFC7F in little-endian, if it matters) and an OR with what is supposed to be a 16-bit-long variable, and then prints the changed bytes the second .bin file. At least, that's what it's
supposed to do...I can't get it right for the life of me. Either I'm misunderstanding the read() function, I need a different function in this case, or it's impossible to do this easily.
Here is the code I'm using (not the entire program, just part of a subroutine):
$i = 0;
$TileData = 0x0000;
while(!(eof(IN)))
{
$BytesRead = read(IN, $TileData, 2);
unless($BytesRead != 2)
{
$NewTileData = (($TileData & 0xFC7F) | ($GFXFileBits));
print OUT $NewTileData;
$i++;
next;
}
}
"$GFXFileBits" is the variable that holds the OR'd bits; it should always have a value of 0x0, 0x80, 0x100, 0x180, 0x200, or 0x280. And yes, I know that $i isn't actually used for anything; it was somewhat of a debugging variable that I haven't removed yet. Can anyone help me out here? I've looked all around for more specific documentation on the read() function and binary file I/O, but I haven't turned up much. I know that code is incorrect, but after a long period of trial and error, I haven't really gotten anywhere.
Oh, and yes, I am using binmode on both the input and output file handles.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.