Help - Search - Member List - Calendar
Full Version: How to handle Null-Charakters
WebHost Freaks Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Angerstein
Hello,

I have a problem reading strings out of a binaery file.

The last 128 Byte of the File contains a String I want to work with.

(sorry, this code is windows, feel free to flame me ^^)
################################
my $tsize = 128;
my $fsize = (-s "d:\mp3\forseti.mp3");
my $offset = ($fsize - $tsize);
open(INF, "d:\mp3\forseti.mp3");
seek(INF, $offset, 0);
$tag = <INF>;
@id3v1 = split (//, $tag);
$id3v_t = unpack("B8","$id3v1[125]");
print $id3v_t;
################################
I get:
QUOTE
00000000

If I print the whole string it looks like:
This is what I get 2002
^^^^^^ ^^^^^^^^^^^ ^^^^^^^^ ^^^^^^^^^^^^^

The Parts I markt are no whitespaces. They are binaery 00000000.

Is there something I could do to transform or remove this null-chars?
(Something small and smart.)

I thought about

$x=0;
foreach @id3v1 ($char){
$testit = unpack("B8", $char);
if ($testit =~ m/0{8}/) {
$id3v1[$x] = "";
}
$x++;
}

but it this is not very smart or small...


Thanks,

Bastian

Dave Gray
On 6/22/05, Angerstein <[Email Removed]> wrote:
QUOTE
I have a problem reading strings out of a binaery file.

The last 128 Byte of the File contains  a String I want to work with.

(sorry, this code is windows, feel free to flame me ^^)
################################
my $tsize = 128;
my $fsize = (-s "d:\mp3\forseti.mp3");
my $offset = ($fsize - $tsize);
open(INF, "d:\mp3\forseti.mp3");
seek(INF, $offset, 0);
$tag = <INF>;
@id3v1 = split (//, $tag);
$id3v_t =  unpack("B8","$id3v1[125]");
print $id3v_t;
################################
I get:
00000000

If I print the whole string it looks like:
This is        what            I get                  2002
^^^^^^      ^^^^^^^^^^^    ^^^^^^^^    ^^^^^^^^^^^^^

The Parts I markt are no whitespaces. They are binaery 00000000.

Is there something I could do to transform or remove this null-chars?

CPAN has modules for working with ID3 tags[1]. That said, you can
replace nulls with a space with:

$id3v_t =~ s/+/ /g;

[1] <http://search.cpan.org/search?query=id3&mode=all>

John W. Krahn
Angerstein wrote:
QUOTE
Hello,

Hello,

QUOTE
I have a problem reading strings out of a binaery file.

perldoc -f binmode


QUOTE
The last 128 Byte of the File contains  a String I want to work with.

(sorry, this code is windows, feel free to flame me ^^)
################################
my $tsize = 128;
my $fsize = (-s "d:\mp3\forseti.mp3");
my $offset = ($fsize - $tsize);
open(INF, "d:\mp3\forseti.mp3");
seek(INF, $offset, 0);
$tag = <INF>;

I would write that as:

use Fcntl ':seek';

my $tsize = 128;
my $file = 'd:/mp3/forseti.mp3';
open INF, '<', $file or die "Cannot open $file: $!";
binmode INF;
seek INF, -$tsize, SEEK_END or die "Cannot seek $file: $!";
read INF, my $tag, $tsize or die "Cannot read $file: $!";


QUOTE
@id3v1 = split (//, $tag);
$id3v_t =  unpack("B8","$id3v1[125]");

Do you really need to split() $tag?

my $id3v_t = unpack 'B8', substr $tag, 125, 1;


QUOTE
print $id3v_t;
################################
I get:

00000000


If I print the whole string it looks like:
This is  what  I get  2002
^^^^^^      ^^^^^^^^^^^    ^^^^^^^^    ^^^^^^^^^^^^^

The Parts I markt are no whitespaces. They are binaery 00000000.

Is there something I could do to transform or remove this null-chars?
(Something small and smart.)

$tag =~ tr// /s;



John
--
use Perl;
program
fulfillment


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.