Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > char->integer, integer->char commands

Reply
Thread Tools

char->integer, integer->char commands

 
 
christophergraber@gmail.com
Guest
Posts: n/a
 
      11-21-2005
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;
}

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      11-21-2005
wrote in news:1132597484.172306.280740
@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/([[digit:]]{2})/chr hex $1/ge;
print "$s\n";

__END__

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

--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
 
 
 
Joel Graber
Guest
Posts: n/a
 
      11-21-2005

writes:
> 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.

--
Joel
 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      11-22-2005
Also sprach :

> 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
--
use bigint;
$n=71423350343770280161397026330337371139054411854 220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($ m+=<=200);
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      11-22-2005
Tassilo v. Parseval wrote:
> Also sprach :
>
>>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;
> }


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

sub ToNumbers {
pack '(a2)*', 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;
> }


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



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      11-22-2005
Also sprach John W. Krahn:
> Tassilo v. Parseval wrote:
>> Also sprach :
>>
>>>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;
>> }

>
> That doesn't create a string of numbers, unpack() returns a list.
>
> sub ToNumbers {
> pack '(a2)*', unpack 'C*', shift;
> }


[...]

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

>>
>> 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
--
use bigint;
$n=71423350343770280161397026330337371139054411854 220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($ m+=<=200);
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How can I change the default Language Firefox uses for File and Menu Commands? Just aFax MAm Firefox 1 11-06-2005 05:45 PM
commands to configure WLAN adapter parameters =?Utf-8?B?SmFnYW4=?= Wireless Networking 0 10-05-2005 11:37 AM
How to Stop Modelsim from echoing tcl commands in batch mode? Dave VHDL 0 09-08-2005 08:47 AM
Need Help Differentiating Bad Commands From Incomplete Commands Tim Stanka Python 1 08-02-2004 02:08 AM
Re: man pages for C commands (GCC commands) Ben Pfaff C Programming 4 06-28-2003 06:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57