LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-09-2011, 02:27 PM   #1
fad216
LQ Newbie
 
Registered: Feb 2011
Posts: 11

Rep: Reputation: Disabled
convert multiple file in directory: ascii to hex in perl


Hello everyone,

I have found a perl script that can convert single file: ascii to hex. However I have thousand of file that I want to convert from ascii to hex.

Here is the perl script that convert single ascii file to hex in single line:
Quote:
#!/usr/bin/perl
# Written by Peter N Lewis a long time ago
# Released in to the Public Domain
# modified by Rudif c/o Perlmonks.org, to handle CRLF conversion

use strict;
use warnings;

usage() if $ARGV[0] and $ARGV[0] =~ m!^-[^-]!;

#our $filepos = 0;
our $linechars = '';

foreach (@ARGV) {
if ($_ eq "-") {
binmode STDIN;
*FILE = *STDIN;
}
else {
open FILE, '<:raw', $_ or die "no such file $_";
}
while (<FILE>) {
dump_char($_) foreach split(//);
}
dump_char( ' ', 1 ) while length($linechars) != 0;
close FILE;
}

sub dump_char {
my ( $char, $blank ) = @_;
if ( length( $linechars ) == 0 ) {
}
$linechars .= ( $char =~ m#[!-~ ]# ) ? $char : '.';
if ( $blank ) {
print ' ';
} else {
printf( "%02X", ord($char) );
}

$linechars = '';
}
So I would like to read multiple file from a directory. Then the file will be have same name file with hex data. Here is sample of the read and write directory file.
Quote:
#!/usr/bin/perl
use Tie::CharArray;
use File::Copy;

$dirtoget="/home/yourname/www/images/";
$dirtowrite="/home/yourname/www/picture/";

opendir(IMD, $dirtoget) || die("Cannot open directory");
@thefiles= readdir(IMD);
closedir(IMD);

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

foreach $f (@thefiles)
{
unless ( ($f eq ".") || ($f eq "..") )
{
print "$f<br />";
}
}

print "</body></html>";
 
Old 04-09-2011, 02:31 PM   #2
corp769
Guru
 
Registered: Apr 2005
Posts: 5,807

Rep: Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996
You could always call the first script from the second script... Have you tried that yet?

Josh
 
Old 04-09-2011, 02:35 PM   #3
fad216
LQ Newbie
 
Registered: Feb 2011
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by corp769 View Post
You could always call the first script from the second script... Have you tried that yet?

Josh
Yes, I have tried use option:

Code:
require 'script.pl';
when I tried, it just only copy file from folder A to folder B...but data inside each data is 0 byte
 
Old 04-10-2011, 12:47 PM   #4
fad216
LQ Newbie
 
Registered: Feb 2011
Posts: 11

Original Poster
Rep: Reputation: Disabled
I have write script for read/write directory. It is work for convert multiple file from directory to another directory with same filename. But the problem is one of multiple data with no data. Another problem is this script is not convert data at same filename. It just put data randomly. Here is the script.

Code:
#use File::Copy;
#program starts
 $dirtoget="/home/user/1";
 $dirtowrite="/home/user/2";

#open $dirtoget folder
opendir(DIR, $dirtoget) || die("Cannot open directory");
 @files = readdir DIR;

our $linechars = '';

 foreach $f (@files){
    unless ( ($f eq ".") || ($f eq "..") ){       
          binmode(FILE);
         
           
        open(FILE, "<$dirtoget/$f");
        print "$f \n";
        	
	while (<FILE>) {
        dump_char($_) foreach split(//);
        }
        dump_char( ' ', 1 ) while length($linechars) != 0;

        # write to file
         $f="$dirtowrite/$f.txt";
	 open(output, ">$f");
	            						     
      }
      close FILE; 
}  
	 sub dump_char 
	{
        my ( $char ) = @_;
        
          $linechars .= ( $char =~ m#[!-~ ]# ) ? $char : '.';
          $hex1 = sprintf ( "%02X", ord($char)) ;   
          $linechars = '';
          print output $hex1 ;             
	 }

close(output);
closedir(DIR);
So how to modified this script for do task properly.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How Can I convert Hex to Binary in a perl script telecom_is_me Programming 18 10-11-2010 04:34 PM
How to convert a hex file to an ASCII file? lxnbie Linux - Server 1 09-08-2010 03:49 AM
Hex output of a hex/ascii input string mlewis Programming 35 04-10-2008 12:05 PM
display in hex + perl + non ASCII characters kshkid Programming 4 02-06-2007 04:48 PM
Convert characters to hex values in perl pjz Programming 3 12-08-2005 08:35 AM


All times are GMT -5. The time now is 04:50 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration