RE: numeric and string conversions
- From: c.rodgers@xxxxxxxxxxxxxxxx (Craig Rodgers)
- Date: Thu, 29 Mar 2007 16:46:14 +1000
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@xxxxxxxxx]
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 myproblem
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.string
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
[\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@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/
.
- Follow-Ups:
- Re: numeric and string conversions
- From: Alan
- Re: numeric and string conversions
- References:
- Re: numeric and string conversions
- From: John W. Krahn
- Re: numeric and string conversions
- Prev by Date: Re: sending output to file and terminal
- Next by Date: RE: perl -s and use strict
- Previous by thread: Re: numeric and string conversions
- Next by thread: Re: numeric and string conversions
- Index(es):
Relevant Pages
|