944,152 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 13239
  • Perl RSS
Sep 24th, 2006
0

Integer to character conversion

Expand Post »
Hello everyone. I'm writing a perl script to implement some active content on my website. Does anyone know if there is a source code library to convert an integer variable to an equivalent character string? I've managed to do the reverse, i.e.

Perl Syntax (Toggle Plain Text)
  1. sub char_2_int{
  2. for ($c = 0; $c < 4; $c++)
  3. {
  4. $d = 3 - $c;
  5. $e = 10**$d;
  6. if ($data[$c] == 0) {}
  7. elsif ($data[$c] == 1) {$hit_count = $hit_count + (1 * $e)}
  8. elsif ($data[$c] == 2) {$hit_count = $hit_count + (2 * $e)}
  9. elsif ($data[$c] == 3) {$hit_count = $hit_count + (3 * $e)}
  10. elsif ($data[$c] == 4) {$hit_count = $hit_count + (4 * $e)}
  11. elsif ($data[$c] == 5) {$hit_count = $hit_count + (5 * $e)}
  12. elsif ($data[$c] == 6) {$hit_count = $hit_count + (6 * $e)}
  13. elsif ($data[$c] == 7) {$hit_count = $hit_count + (7 * $e)}
  14. elsif ($data[$c] == 8) {$hit_count = $hit_count + (8 * $e)}
  15. elsif ($data[$c] == 9) {$hit_count = $hit_count + (9 * $e)}
  16. }
  17. }

where $data and $hit_count are the character and integer variables respectively. The int_2_char job is proving a bit harder. Any help would be appriciated. Thanks.

Steven.
Similar Threads
Reputation Points: 47
Solved Threads: 1
Posting Whiz in Training
Mushy-pea is offline Offline
271 posts
since Jun 2006
Sep 24th, 2006
0

Re: Integer to character conversion

I don't understand what that sub routine does or what you are asking. Please explain your question some more.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Sep 24th, 2006
0

Re: Integer to character conversion

Hello Kevin. What I mean is, is there a library function that you can pass:

$variable_1 = 1234 (type: integer)

and it will return:

$variable_2 = "1234" (type: character)

Steven.
Reputation Points: 47
Solved Threads: 1
Posting Whiz in Training
Mushy-pea is offline Offline
271 posts
since Jun 2006
Sep 24th, 2006
0

Re: Integer to character conversion

After doing some reading I've found the answer:

Perl Syntax (Toggle Plain Text)
  1. $variable_2 = sprintf("%u", $variable_1);

Where $variable_2 and $variable_1 are of type character and integer, respectively.
Reputation Points: 47
Solved Threads: 1
Posting Whiz in Training
Mushy-pea is offline Offline
271 posts
since Jun 2006
Sep 27th, 2006
0

Re: Integer to character conversion

One of the beautiful things AND at the same time, one of the ugly things about perl is that it does conversions on the fly - by YOUR usage.

here's a snip of calendar code that converts an integer to a character with leading zero:

foreach $day (@mymonth) {
my $LinkDay=$mymonth[$counter];
if ($LinkDay < 10) {
$LinkDay = "0$LinkDay";
}

to get chars to ints... i've just used them as such.

g'luck!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vonfeldt is offline Offline
8 posts
since Jul 2005
Sep 28th, 2006
0

Re: Integer to character conversion

perl will drop leading zeros in numbers, but you can stringify numbers to retain the leading zeros or use sprintf() or printf() to pad numbers with leading zeros.

perl will almost always know when to treat digitis as numbers or strings.

Perl Syntax (Toggle Plain Text)
  1. my $var = '01';
  2. my $var2 = '03';
  3. print $var + $var2;

prints: 4

Perl Syntax (Toggle Plain Text)
  1. my $var = '01';
  2. my $var2 = '03';
  3. print $var . $var2;

prints: 0103
Last edited by KevinADC; Sep 28th, 2006 at 2:58 pm.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Sep 28th, 2006
0

Re: Integer to character conversion

Thanks for the advice. I guess this means I wasted my effort with that little subroutine. However, at least you know for sure whats going to happen when you implement the conversion yourself.

Steven.
Reputation Points: 47
Solved Threads: 1
Posting Whiz in Training
Mushy-pea is offline Offline
271 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: noob on perl... please help
Next Thread in Perl Forum Timeline: Simple website hit counter





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC