But not sorry enough to do as requested, it seems.
You still have not read the Posting Guidlines for this group. Or, you
have but have chosen not to follow them. They are posted twice a week.
If you can't find them on your news server, use Google Groups' archive
to find them.
I tried reading the FAQ, too cryptic.
Be more specific. Which FAQ did you read, and in what way did you find
it too cryptic? What part did you not understand.
I am not writing here
out of laziness and wanting a direct answer.
It is out of frustration and wanting to learn. I looked at a lot of
documentation - pack, unpack, int, hex ... perldoc -f ...
And which part of these were unclear to you as well?
That shebang looks mighty suspicious. Are you running your script from
the root directory? Or do you have a usr/bin/ in your current
directory? Or are you retyping instead of copy and pasting?
use strict;
my @file;
my $v;
my $i;
Your variables should always be declared in the smallest scope
possible.
open(FILE,"< values") or die "cannot open file for reading: $!";
Please use lexical filehandles and the three-argument form of open.
open my $file, '<', 'values' or die "Can't open values: $!";
As suggested in a previous response, there is no reason to read your
entire file into memory just to process it line by line later on. Use a
close (FILE) or die "an error occured while trying to close the file:
$!";
print ("@file");
Weren't you curious as to why you got extra spaces in your output?
perldoc -q spaces
print ("\n");
foreach $v (@file){
chomp($v);
printf ("%0.4x ",$v);
}
$v is a string consisting of digits, for example '1111'. This line
takes the number one thousand, one hundred and eleven and converts it
into its hexadecimal representation.
You want a function that you can tell "this string consists of binary
data. Give me the number it represents". Now, just from experience
reading this group, I would guess that involves the use of pack()
and/or unpack(). However, I assure you I am 100% pack-clueless. I've
never understood it. I've read the docs, I've read the tutorials, I
just can't grok it.
Be that as it may, however, I did decide to check the FAQ to see if
this all-too-common task has been asked about before. I typed:
perldoc -q bits
and I was given the following question:
How do I convert bits into ints?
Well, low and behold, that's exactly what we're trying to do! The
answer given in that FAQ is:
$decimal = unpack('c', pack('B8', '10110110'));
Okay. Let's try that with your data:
$decimal = unpack('c', pack('B8', '0110'));
Hrm. 96? That doesn't seem quite right. However, I do note that
01100000 *is* the representation for 96. Is it possible that this
pack() magic is auto-padding the right-hand part of the string with 0s
until it has a full byte? Certainly seems that way. Let's try
specifying the full byte:
$decimal = unpack('c', pack('B8', '00000110'));
Ah! There we go! Now we get our correct answer of 6!
Well, now that we know the correct pack/unpack incantation, this
shouldn't be too hard...
it shoudn't be hard at all write, I am reading in a string/variable
1111 and I want to print out f
this still doesn't work : (
"doesn't work" is a phenomenally unhelpful error description. Once
again, please READ the Posting Guidelines for this group.
#!/usr/bin/perl
use strict;
use warnings;
while (my $bits = <DATA>){
chomp($bits);
$bits = '0000' . $bits if length $bits == 4;
my $dec = unpack ('c', pack('B8', $bits));
my $hex = sprintf '%.4x', $dec;
#the intermediary step could be eliminated by
#using 'h' as the unpack template instead of 'c'
print "$bits ==> $dec ==> $hex\n";
}
__DATA__
1111
0101
0110
1010
01101111
Paul Lalli