C
christophergraber
Hi,
I created two subroutines, I want to know if there is a better, faster
way to do this. I just use ord() and chr():
1) Take a upper case string like "GRABER" and covert it to its ASCII
equivalent (a string of 2 digit numbers, concatenated) - 718265666982
# Convert a string of A-Z Characters to 2 digit ASCII numbers, one
after another
sub ToNumbers {
my $character_string = shift;
# remove whitespace at beginning and end of name?
# what about spaces or dashe's IN the name?
my $numbers_string;
for ($i=0; $i < length($character_string); $i++) {
$numbers_string = $numbers_string . ord(substr($character_string,
$i, 1));
}
return $numbers_string;
}
2) Take a string like "718265666982" and convert it to a string of
characters like "GRABER"
# Converts a string of two digit ASCII codes to a string of characters
sub ToCharacters {
my $number_string = shift;
# remove whitespace at beginning and end of numbers?
# what about spaces or anything else in the numbers?
my $characters_string;
for ($i=0; $i < length($number_string); $i=$i+2) {
print "i is $i \n";
$characters_string = $characters_string .
chr(substr($number_string, $i, 2));
}
return $characters_string;
}
I created two subroutines, I want to know if there is a better, faster
way to do this. I just use ord() and chr():
1) Take a upper case string like "GRABER" and covert it to its ASCII
equivalent (a string of 2 digit numbers, concatenated) - 718265666982
# Convert a string of A-Z Characters to 2 digit ASCII numbers, one
after another
sub ToNumbers {
my $character_string = shift;
# remove whitespace at beginning and end of name?
# what about spaces or dashe's IN the name?
my $numbers_string;
for ($i=0; $i < length($character_string); $i++) {
$numbers_string = $numbers_string . ord(substr($character_string,
$i, 1));
}
return $numbers_string;
}
2) Take a string like "718265666982" and convert it to a string of
characters like "GRABER"
# Converts a string of two digit ASCII codes to a string of characters
sub ToCharacters {
my $number_string = shift;
# remove whitespace at beginning and end of numbers?
# what about spaces or anything else in the numbers?
my $characters_string;
for ($i=0; $i < length($number_string); $i=$i+2) {
print "i is $i \n";
$characters_string = $characters_string .
chr(substr($number_string, $i, 2));
}
return $characters_string;
}