For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2007 > Re: numeric and string conversions









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: numeric and string conversions
John W. Krahn

2007-03-29, 4:00 am

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
Craig Rodgers

2007-03-29, 4:00 am

Thanks,

The chr function was what I needed.

It seems like there are a lot of "stone cutter" functions/operators in perl,
I don't suppose you could recommend somewhere in the docs, on the web or in
a book that would be useful in getting up to speed with these functions?

At the moment it seems to me like the only way to find these things is to
troll through all the docs hoping that you stumble across the right one.
There must be a better way to do it!!

Cheers and thanks again

Craig

-----Original Message-----
From: John W. Krahn [mailto:krahnj@telus.net]
Sent: Thursday, 29 March 2007 3:37 PM
To: Perl Beginners
Subject: Re: numeric and string conversions

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: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/




Alan

2007-03-29, 9:58 pm

On Wednesday 28 March 2007 23:46, Craig Rodgers wrote:
> Thanks,
>
> The chr function was what I needed.
>
> It seems like there are a lot of "stone cutter" functions/operators in
> perl, I don't suppose you could recommend somewhere in the docs, on the web
> or in a book that would be useful in getting up to speed with these
> functions?


I don't know about "up to speed". But at least the next lists them and what
they are for. (Perhaps more than that -- but, so far, I just took a quick,
short look).

http://perldoc.perl.org/index-functions.html

http://perldoc.perl.org/perlop.html

http://perldoc.perl.org/perl.html

--
Alan.
Craig Rodgers

2007-03-29, 9:58 pm


Thank you, I can't believe I hadn't found this before!


-----Original Message-----
From: Alan [mailto:acelists@gmail.com]
Sent: Friday, 30 March 2007 11:39 AM
To: beginners@perl.org
Subject: Re: numeric and string conversions

On Wednesday 28 March 2007 23:46, Craig Rodgers wrote:
> Thanks,
>
> The chr function was what I needed.
>
> It seems like there are a lot of "stone cutter" functions/operators in
> perl, I don't suppose you could recommend somewhere in the docs, on the

web
> or in a book that would be useful in getting up to speed with these
> functions?


I don't know about "up to speed". But at least the next lists them and what

they are for. (Perhaps more than that -- but, so far, I just took a quick,
short look).

http://perldoc.perl.org/index-functions.html

http://perldoc.perl.org/perlop.html

http://perldoc.perl.org/perl.html

--
Alan.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/




Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com