Craig Rodgers wrote:
> Hi,

Hello,

> Please bear with me for a bit, I'm not realy sure how to explaine my problem
> so I'll try to give you an example of what's going wrong and what the
> desired behaviour is.
> 
> I'm trying to calculate the CRC-8 checksum for the numbers 0~16 using the
> Digest::CRC perl module.
> 
> The crc8 function takes a scalar input value and should return the crc
> value. 
> 
> It apears that the crc8 function uses the string representation of the
> scalar value. Ie
>  
>       printf ("%i,",crc8(0x01)) returns 144 which is the crc8 value of the
> letter '1'.

string '1'


>       printf ("%i,",crc8("\x01")); returns 7 which is the correct crc8
> value                 for the number 1.

string "\x01"


> So far so good I hope.
> 
> The problem I'm having is that I'd like to make a simple loop to iterate
> through the values I'd like to calculate the crc of.
> 
> use Digest::CRC qw(crc8);
> 
> foreach (0..16){
>       print("$_,");
>       printf ("%i,\n\r", crc8($_));
> }
> 
> I've tried every possible combination and permitation of escape charaters,
> adding "+ 0" to force a numeric conversion etc. To no avail.
> 
> I think what I want to do is to create a string that contains a single
> character that consits of the binary value of my loop counter. Ie the string
> [\x01] - the binary value of 1 as opposed to the ASCII/character value of
> it. 
> 
> Please any sugestions of how to perform such a task?

In Perl numbers and strings that look like numbers are interchangeable:

$ perl -le'print for -4 + 9, q[-4] + 9, -4 + q[9], q[-4] + q[9]'
5
5
5
5


It looks like you want to convert the numbers to characters:

foreach ( 0 .. 16 ) {
    print "$_,";
    printf "%i,\n\r", crc8( chr );
}


perldoc -f chr



John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to