Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Efficiently convert characters to octal representation

Reply
Thread Tools

Efficiently convert characters to octal representation

 
 
John W. Krahn
Guest
Posts: n/a
 
      07-29-2006
Worky Workerson wrote:
> I have a (possibly binary) string like "worky" where I'd like to
> convert each byte to its octal representation, resulting in a string
> "\167\157\162\153\171". I have two solutions, however I'm looking for
> any way that would be faster.
>
> Control:
> $content = 'worky';
> return $content;
>
> Solution 1 (in place w/regex):
> $content = 'worky';
> $content =~ s/(.|\n)/sprintf("\\%03o", ord $1)/eg;
> return $content
>
> Solution 2 (index into string):
> $content = 'worky';
> do {
> use bytes;
> foreach my $idx (0..(length($content)-1)) {
> $ret .= sprintf("\\%03o", ord(substr($content, $idx, 1)));
> }
> };
> return $ret;
>
> Based on a quick cmpthese benchmark, the control is about 16 times
> faster than solution 1 and about 9 times faster than solution 2.
>
> Does anyone know of A) The fastest way to do this or B) some
> tips/tricks on how to speedup my methods?


Create the translation table first:

my %table = map { chr, sprintf '\%03o', $_ } 0 .. 255;


$content =~ s/(.)/$table{$1}/sg;


foreach my $idx ( 0 .. length( $content ) - 1 ) {
$ret .= $table{ substr $content, $idx, 1 };
}




John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      07-29-2006

Quoth http://www.velocityreviews.com/forums/(E-Mail Removed):
> "Sisyphus" <(E-Mail Removed)> wrote:
> >
> > D:\pscrpt\inline\>perl char2octal.pl
> > Use of uninitialized value in subroutine entry at char2octal.pl line 22.
> > \141\142\143\144\101\102\103\104
> > -----------------------------
> >
> > I'm sure it's one of those questions that will make me go "Doh!", but I
> > can't for the life of me see what is causing that "uninitialized"
> > warning. Any hints ? (I'm running perl 5.8.8 on Win32.)

>
> Ah, I forgot to turn on warnings and so never saw it.
>
> Apparently sv_catpvf, unlike .= operator, doesn't care for undefined
> values. So make that:
>
> ret = newSV(4*len);
> sv_setpv(ret, "");
> for (i=0; i<len; i++,s++) {
>
> I guess Inline warnings all get reported as being at subroutine entry?


Yes, as with all warnings thrown inside XS. The currently executing Perl
op is the sub call, so that's what you get: the whole XS sub is run as
part of the sub call op, which then returns rather than jumping to the
start of the sub as it would with Perl.

> For what it's worth, I've made another uglier one that is about twice again
> as fast. This is going to wrap like crazy:
>
> SV* sol32(SV* a) {
> static const char * cache[]={"\\000","\\001","\\002","\\003","\\004",


I wondered about that (in Perl, not in C); to make it a little less ugly
you could have

static const char cache[0x100][5]; /* c arrays confuse me */

void populate_cache (void) {
int i;
for (i=0; i<0x100; i++) {
Copy(form("\\%3o", i), cache[i], 5, char);
}
}

Ben

--
Outside of a dog, a book is a man's best friend.
Inside of a dog, it's too dark to read.
(E-Mail Removed) Groucho Marx
 
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
How to convert HTML special characters to the real characters with a Java script Stefan Mueller HTML 3 07-23-2006 10:09 PM
Convert potion of string to Octal Bill H Perl Misc 10 01-04-2006 12:02 AM
How to convert a string like '777' to an octal integer like 0777? KB Python 5 07-31-2005 11:05 AM
Convert Raw Text Escaped Characters to Characters nicholas.wakefield@gmail.com Java 2 07-11-2005 09:17 PM
converting characters to octal Hostos Java 7 10-15-2003 06:07 AM



Advertisments