Angerstein
Jun 22 2005, 07:08 AM
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:
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
Jun 22 2005, 01:06 PM
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
Jun 23 2005, 04:55 AM
Angerstein wrote:
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
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please
click here.