CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Writing a binary file

 



perlisme
Novice

Jan 31, 2010, 5:54 PM

Post #1 of 14 (1716 views)
Writing a binary file Can't Post

I have a hex dump of a binary file and would like to output it (its in an array) back into a binary file. What is the best way to do that?

Thanks!!


FishMonger
Veteran / Moderator

Jan 31, 2010, 8:42 PM

Post #2 of 14 (1707 views)
Re: [perlisme] Writing a binary file [In reply to] Can't Post

See:
perldoc -f open
perldoc -f print
perldoc -f printf
perldoc -f sprintf

perldoc -f sysopen
perldoc -f syswrite


(This post was edited by FishMonger on Jan 31, 2010, 8:43 PM)


perlisme
Novice

Feb 1, 2010, 7:32 AM

Post #3 of 14 (1692 views)
Re: [FishMonger] Writing a binary file [In reply to] Can't Post

Thanks for the references!


Code
@teb = (0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 
0x00, 0x07, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
0x30, 0x07, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
0x30, 0x07, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
0x30, 0x07, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2e);

open(FILE, ">:raw", "a.teb") or die;
print FILE @teb;
close(FILE);


This doesn't work. What am I doing wrong?


FishMonger
Veteran / Moderator

Feb 1, 2010, 8:01 AM

Post #4 of 14 (1689 views)
Re: [perlisme] Writing a binary file [In reply to] Can't Post

Use printf to format the data.

I'm not exactly sure which format you want in the file, but here's an example.

Code
open my $FILE, ">:raw", "a.teb" or die; 

foreach my $num ( @teb ) {
printf $FILE "%o", $num; # an unsigned integer, in octal
# printf $FILE "%x", $num; # an unsigned integer, in hexadecimal
# printf $FILE "%b", $num; # an unsigned integer, in binary
}
close($FILE);



perlisme
Novice

Feb 1, 2010, 8:37 AM

Post #5 of 14 (1686 views)
Re: [FishMonger] Writing a binary file [In reply to] Can't Post


Code
@teb = (0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 
0x00, 0x07, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
0x30, 0x07, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
0x30, 0x07, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
0x30, 0x07, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2e);

foreach my $num ( @teb ) {
# printf $FILE "%o", $num; # an unsigned integer, in octal
# printf $FILE "%x", $num; # an unsigned integer, in hexadecimal
printf $FILE "%b", $num; # an unsigned integer, in binary
}
close($FILE);



Code
Can't use an undefined value as a symbol reference at hex.pl line 10.


Issue?


FishMonger
Veteran / Moderator

Feb 1, 2010, 8:42 AM

Post #6 of 14 (1683 views)
Re: [perlisme] Writing a binary file [In reply to] Can't Post

I'm unable to duplicate your issue.

Quote
[root@fc4dev ~]# ls a.teb
ls: a.teb: No such file or directory
[root@fc4dev ~]# cat test.pl

Code
#!/usr/bin/perl 

use strict;
use warnings;

my @teb = (0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x07, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
0x30, 0x07, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
0x30, 0x07, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
0x30, 0x07, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2e);

open my $FILE, ">:raw", "a.teb" or die $!;

foreach my $num ( @teb ) {
# printf $FILE "%o", $num; # an unsigned integer, in octal
# printf $FILE "%x", $num; # an unsigned integer, in hexadecimal
printf $FILE "%b", $num; # an unsigned integer, in binary
}
close($FILE);

[root@fc4dev ~]# ./test.pl
[root@fc4dev ~]# cat a.teb
000000010111110000101110110000101110110000101110110000111110000101110110000101110110000101110110000111110000101110110000101110110000101110110000111110000101110110000101110110000101110[root@fc4dev ~]#



(This post was edited by FishMonger on Feb 1, 2010, 8:43 AM)


FishMonger
Veteran / Moderator

Feb 1, 2010, 8:46 AM

Post #7 of 14 (1681 views)
Re: [perlisme] Writing a binary file [In reply to] Can't Post

Please post a complete script that demonstrates the issue.


perlisme
Novice

Feb 1, 2010, 8:55 AM

Post #8 of 14 (1679 views)
Re: [FishMonger] Writing a binary file [In reply to] Can't Post

Let me see if I can explain better what I want to do...

I have a file, a.teb (attached)

I opened it in a hex editor and dumped it into an array.

Now I want to output the dump from perl back into the file

So maybe I want to use hex instead of binary?

Thank you for your help!


(This post was edited by perlisme on Feb 1, 2010, 8:55 AM)
Attachments: a.teb (37 B)


FishMonger
Veteran / Moderator

Feb 1, 2010, 10:22 AM

Post #9 of 14 (1675 views)
Re: [perlisme] Writing a binary file [In reply to] Can't Post


Quote
I opened it in a hex editor and dumped it into an array.

Can you give more details on how you did that?


Quote
Now I want to output the dump from perl back into the file

Why are you taking these steps to dump then put the exact same data back into the same file?

Can you explain what you really need to accomplish?


perlisme
Novice

Feb 1, 2010, 10:58 AM

Post #10 of 14 (1673 views)
Re: [FishMonger] Writing a binary file [In reply to] Can't Post

I want to be able to modify certain values in the hex dump array and output it back in a file.

What am I doing wrong?


FishMonger
Veteran / Moderator

Feb 1, 2010, 11:42 AM

Post #11 of 14 (1671 views)
Re: [perlisme] Writing a binary file [In reply to] Can't Post


In Reply To
I want to be able to modify certain values in the hex dump array and output it back in a file.

What am I doing wrong?


That's not a clear explanation of what you really need to accomplish.

There's no way for us to tell you what you're doing wrong, because you haven't provided enough info.

How, exactly, are you reading in the source file and assigning the data to the array?

Which values do you want to modify?

In what way do you want to modify the values?

Please answer those questions and post your entire script.


perlisme
Novice

Feb 1, 2010, 11:51 AM

Post #12 of 14 (1669 views)
Re: [FishMonger] Writing a binary file [In reply to] Can't Post

Lets pretend I just have this hex data in my array and I want to output the hex into a file.


Code
@teb = (0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 
0x00, 0x07, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
0x30, 0x07, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
0x30, 0x07, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
0x30, 0x07, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2e);

foreach my $num ( @teb ) {
# printf $FILE "%o", $num; # an unsigned integer, in octal
# printf $FILE "%x", $num; # an unsigned integer, in hexadecimal
printf $FILE "%b", $num; # an unsigned integer, in binary
}
close($FILE);


Basically I want the perl version of this:


Code
#include <stdio.h> 

int main()
{

char teb[] =
{
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00,
0x07, 0x30, 0x2e, 0x30,
0x2e, 0x30, 0x2e, 0x30,
0x07, 0x30, 0x2e, 0x30,
0x2e, 0x30, 0x2e, 0x30,
0x07, 0x2e, 0x30, 0x2e,
0x30, 0x2e, 0x30, 0x07,
0x2e, 0x30, 0x2e, 0x30,
0x2e
};

FILE *fd;
fd = fopen("a.teb", "w");
int i;
for(i=0; i<sizeof(teb); i++) {
fprintf(fd, "%c", teb); }

fclose(fd);

return 0;
}


I want to output this hex in @teb into a file. How can I do that?


(This post was edited by perlisme on Feb 1, 2010, 12:00 PM)


FishMonger
Veteran / Moderator

Feb 1, 2010, 1:46 PM

Post #13 of 14 (1664 views)
Re: [perlisme] Writing a binary file [In reply to] Can't Post


In Reply To
Lets pretend I just have this hex data in my array and I want to output the hex into a file.

Basically I want the perl version of this:


Code
#include <stdio.h> 

int main()
{

char teb[] =
{
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00,
0x07, 0x30, 0x2e, 0x30,
0x2e, 0x30, 0x2e, 0x30,
0x07, 0x30, 0x2e, 0x30,
0x2e, 0x30, 0x2e, 0x30,
0x07, 0x2e, 0x30, 0x2e,
0x30, 0x2e, 0x30, 0x07,
0x2e, 0x30, 0x2e, 0x30,
0x2e
};

FILE *fd;
fd = fopen("a.teb", "w");
int i;
for(i=0; i<sizeof(teb); i++) {
fprintf(fd, "%c", teb); }

fclose(fd);

return 0;
}


I want to output this hex in @teb into a file. How can I do that?



Do you understand that code?

If so, then it should be clear that Perl's printf function accomplishes the same thing as the fprintf statement.

As I stated when I posted the example, I didn't know what format you wanted to use. Your fprintf statement clearly shows that you want to use the "%c" format.


perlisme
Novice

Feb 1, 2010, 4:23 PM

Post #14 of 14 (1661 views)
Re: [FishMonger] Writing a binary file [In reply to] Can't Post

Gotcha, thanks!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives