char->integer, integer->char commands

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;
}
 
A

A. Sinan Unur

(e-mail address removed) wrote in @f14g2000cwb.googlegroups.com:
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

Are those hex digits?
# Convert a string of A-Z Characters to 2 digit ASCII numbers, one
after another
sub ToNumbers {

What you have here is an attempt to write C in Perl.

#!/usr/bin/perl

use strict;
use warnings;

my $s = 'GRABER';
$s =~ s/([A-Z])/sprintf '%2.2X', ord $1/ge;

print "$s\n";

$s =~ s/([[:xdigit:]]{2})/chr hex $1/ge;
print "$s\n";

__END__

D:\Home\asu1\UseNet\clpmisc> c
475241424552
GRABER
 
J

Joel Graber

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

2) Take a string like "718265666982" and convert it to a string of
characters like "GRABER"

Perl is faster when iteration is implicit.
s/(.)/f($1)/g; # iterates by character nicely.

#!/usr/local/bin/perl
use warnings;
use strict;
my (%H,%h);
%H = reverse (%h=qw( G 71 R 82 A 65 B 66 E 69));
$_ = "GRABER\n";
print;
s/(.)/$h{$1}/g; # string to ASCII concatenated
print;
s/(..)/$H{$1}/g; # ASCII concat string to ASCII
print;

# prints
#GRABER
#718265666982
#GRABER
Note that dot (.) did not match \n at end of input line

These also work, for the given input string.
s/(.)/ord($1)/ge; # string to ASCII concatenated
s/(..)/chr($1)/ge; # and back

Perl is faster when iteration is implicit.
 
T

Tassilo v. Parseval

Also sprach (e-mail address removed):
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

Use unpack with the 'C' template. Your ToNumbers function then becomes:

sub ToNumbers {
unpack "C*", shift;
}
# 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"

Use pack:

sub ToCharactes {
pack "C*", shift;
}

See `perldoc -f pack` for a list of the various available templates.
There's also a tutorial on pack worth reading as pack/unpack can
sometimes be tricky to use correctly: `perldoc perlpacktut`.

Tassilo
 
J

John W. Krahn

Tassilo said:
Also sprach (e-mail address removed):


Use unpack with the 'C' template. Your ToNumbers function then becomes:

sub ToNumbers {
unpack "C*", shift;
}

That doesn't create a string of numbers, unpack() returns a list.

sub ToNumbers {
pack '(a2)*', unpack 'C*', shift;
}

Use pack:

sub ToCharactes {
pack "C*", shift;
}

sub ToCharactes {
pack 'C*', unpack '(a2)*', shift;
}



John
 
T

Tassilo v. Parseval

Also sprach John W. Krahn:
Tassilo said:
Also sprach (e-mail address removed):


Use unpack with the 'C' template. Your ToNumbers function then becomes:

sub ToNumbers {
unpack "C*", shift;
}

That doesn't create a string of numbers, unpack() returns a list.

sub ToNumbers {
pack '(a2)*', unpack 'C*', shift;
}
[...]
Use pack:

sub ToCharactes {
pack "C*", shift;
}

sub ToCharactes {
pack 'C*', unpack '(a2)*', shift;
}

You're right, sorry for the sloppy posting. I essentially only tested
things with 'print' and it worked (because it prints lists naturally).

Tassilo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top