Integer to character conversion

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2006
Posts: 263
Reputation: Mushy-pea is an unknown quantity at this point 
Solved Threads: 1
Mushy-pea's Avatar
Mushy-pea Mushy-pea is offline Offline
Posting Whiz in Training

Integer to character conversion

 
0
  #1
Sep 24th, 2006
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.

  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.
The one question you should not ask when teaching a new language structure is "Do you understand?". Do you understand?
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Integer to character conversion

 
0
  #2
Sep 24th, 2006
I don't understand what that sub routine does or what you are asking. Please explain your question some more.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 263
Reputation: Mushy-pea is an unknown quantity at this point 
Solved Threads: 1
Mushy-pea's Avatar
Mushy-pea Mushy-pea is offline Offline
Posting Whiz in Training

Re: Integer to character conversion

 
0
  #3
Sep 24th, 2006
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.
The one question you should not ask when teaching a new language structure is "Do you understand?". Do you understand?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 263
Reputation: Mushy-pea is an unknown quantity at this point 
Solved Threads: 1
Mushy-pea's Avatar
Mushy-pea Mushy-pea is offline Offline
Posting Whiz in Training

Re: Integer to character conversion

 
0
  #4
Sep 24th, 2006
After doing some reading I've found the answer:

  1. $variable_2 = sprintf("%u", $variable_1);

Where $variable_2 and $variable_1 are of type character and integer, respectively.
The one question you should not ask when teaching a new language structure is "Do you understand?". Do you understand?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 8
Reputation: vonfeldt is an unknown quantity at this point 
Solved Threads: 0
vonfeldt vonfeldt is offline Offline
Newbie Poster

Re: Integer to character conversion

 
0
  #5
Sep 27th, 2006
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!
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Integer to character conversion

 
0
  #6
Sep 28th, 2006
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.

  1. my $var = '01';
  2. my $var2 = '03';
  3. print $var + $var2;

prints: 4

  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 263
Reputation: Mushy-pea is an unknown quantity at this point 
Solved Threads: 1
Mushy-pea's Avatar
Mushy-pea Mushy-pea is offline Offline
Posting Whiz in Training

Re: Integer to character conversion

 
0
  #7
Sep 28th, 2006
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.
The one question you should not ask when teaching a new language structure is "Do you understand?". Do you understand?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 9474 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Perl
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC