RE: numeric and string conversions



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




.



Relevant Pages

  • Re: numeric and string conversions
    ... Digest::CRC perl module. ... The crc8 function takes a scalar input value and should return the crc ... It apears that the crc8 function uses the string representation of the ... perldoc -f chr ...
    (perl.beginners)
  • Cant Compiling perl5.8.8 on FreeBSD6.2
    ... WITH_DEBUGGING=yes Build perl with debugging support. ... First let's make sure your kit is complete. ... What is the file extension used for shared libraries? ... I'll use sprintf to convert floats into a string. ...
    (comp.unix.bsd.freebsd.misc)
  • Re: use of DBI; I am getting multiple error messages mixed in with ?the correct output.
    ... But I'm not talking about C++ or Java or SQL. ... This is a perl newsgroup. ... to means by a "defined null string", as opposed to an undefined value. ... And where exactly are you getting the idea that the empty string is a ...
    (comp.lang.perl.misc)
  • Re: Perl Strings vs FileHandle
    ... Just wanted to run this through Perl gurus to see if fit is ... testing it every time through the loop. ... The comparison test to the string '1' is superfluous. ... Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers ...
    (comp.lang.perl.misc)
  • Re: Working with Source Code to Insert Copyright Statements as a Header
    ... Paul Lalli wrote: ... Where could I go to find some samples of PERL code that do the ... ascii english text, etc etc etc) at the top of the file, from a string: ... `perldoc Tie::File` at the command line to read its documentation. ...
    (comp.lang.perl.misc)