Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Hex strings treated differently when read from STDIN?

Reply
Thread Tools

Hex strings treated differently when read from STDIN?

 
 
fhscobey
Guest
Posts: n/a
 
      11-01-2005
Hi,
Just curious if anyone knows why the following happens:

Example 1:
$ perl -e 'print("\x{4A}\x{65}\x{66}\x{66}","\n");'
Jeff

Example 2:
$ echo '\x{4A}\x{65}\x{66}\x{66}' | perl -e
'while(<STDIN>){print("$_");}'
\x{4A}\x{65}\x{66}\x{66}

Example 3:
$ echo '\x{4A}\x{65}\x{66}\x{66}' | perl -e 'while(<STDIN>){$line="$_";
chomp($line); @chars=split(//,"$line"); foreach $ch
(@chars){print($ch,"|");}}'
\|x|{|4|A|}|\|x|{|6|5|}|\|x|{|6|6|}|\|x|{|6|6|}|

Not sure why Perl doesn't recognize the string in Ex. 2 as a hex
string. Anyone know why? Also, how would one iterate over the
individual chars of the hex string in Ex. 3? I'm assuming the answer
to #2 has something to do with #3.

I'm using RedHat Linux 7.2, perl 5.8.1.

Thanks for any assistance you can offer!
- Jeff

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      11-01-2005
"fhscobey" <> wrote in
news: ps.com:

> Hi,
> Just curious if anyone knows why the following happens:
>
> Example 1:
> $ perl -e 'print("\x{4A}\x{65}\x{66}\x{66}","\n");'
> Jeff
>
> Example 2:
> $ echo '\x{4A}\x{65}\x{66}\x{66}' | perl -e
> 'while(<STDIN>){print("$_");}'
> \x{4A}\x{65}\x{66}\x{66}
>
> Example 3:
> $ echo '\x{4A}\x{65}\x{66}\x{66}' | perl -e
> 'while(<STDIN>){$line="$_"; chomp($line); @chars=split(//,"$line");
> foreach $ch (@chars){print($ch,"|");}}'
> \|x|{|4|A|}|\|x|{|6|5|}|\|x|{|6|6|}|\|x|{|6|6|}|
>
> Not sure why Perl doesn't recognize the string in Ex. 2 as a hex
> string. Anyone know why?


From perldoc perlop:

The following escape sequences are available in constructs that
interpolate and in transliterations.

\t tab (HT, TAB)
\n newline (NL)
\r return (CR)
\f form feed (FF)
\b backspace (BS)
\a alarm (bell) (BEL)
\e escape (ESC)
\033 octal char (ESC)
\x1b hex char (ESC)
\x{263a} wide hex char (SMILEY)
\c[ control char (ESC)
\N{name} named Unicode character

A double-quoted string in program source interpolates. Input does not.

You can interpret the escape sequences yourself:

#!/usr/bin/perl

use strict;
use warnings;

while (my $line = <STDIN>) {
my $str;
while ($line =~ m/ \\x{([[digit:]]+)} /xg) {
$str .= chr(hex $1);
}
print "$str\n";
}
__END__

D:\Home\asu1\UseNet\clpmisc> bug
\x{73}\x{69}\x{6e}\x{61}\x{6e}
sinan

Or:

#!/usr/bin/perl

use strict;
use warnings;

while (my $line = <DATA>) {
$line =~ s/ \\x{([[digit:]]+)} / chr(hex $1) /xge;
print $line;
}

__DATA__
\x{53}\x{69}\x{6e}\x{61}\x{6e}

--
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
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      11-01-2005
fhscobey <> wrote:

> Example 2:
> $ echo '\x{4A}\x{65}\x{66}\x{66}' | perl -e



> Not sure why Perl doesn't recognize the string in Ex. 2 as a hex
> string.



Probably because it isn't one.


> Anyone know why?



Have a look at the data that you are passing to perl:

echo '\x{4A}\x{65}\x{66}\x{66}' | cat

and

echo '\x{4A}\x{65}\x{66}\x{66}' | wc -c

You are passing a 25-character long string to perl when I think
you were trying to pass it a 4-character long string.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-01-2005
fhscobey <> wrote:

> 'while(<STDIN>){print("$_");}'

^ ^
^ ^

perldoc -q vars


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Dave
Guest
Posts: n/a
 
      11-01-2005

"fhscobey" <> wrote in message
news: ps.com...
> Hi,
> Just curious if anyone knows why the following happens:
>
> Example 1:
> $ perl -e 'print("\x{4A}\x{65}\x{66}\x{66}","\n");'
> Jeff
>
> Example 2:
> $ echo '\x{4A}\x{65}\x{66}\x{66}' | perl -e
> 'while(<STDIN>){print("$_");}'
> \x{4A}\x{65}\x{66}\x{66}
>
> Example 3:
> $ echo '\x{4A}\x{65}\x{66}\x{66}' | perl -e 'while(<STDIN>){$line="$_";
> chomp($line); @chars=split(//,"$line"); foreach $ch
> (@chars){print($ch,"|");}}'
> \|x|{|4|A|}|\|x|{|6|5|}|\|x|{|6|6|}|\|x|{|6|6|}|
>
> Not sure why Perl doesn't recognize the string in Ex. 2 as a hex
> string. Anyone know why? Also, how would one iterate over the
> individual chars of the hex string in Ex. 3? I'm assuming the answer
> to #2 has something to do with #3.
>
> I'm using RedHat Linux 7.2, perl 5.8.1.
>
> Thanks for any assistance you can offer!
> - Jeff
>


The short answer is that perl does not interpolate recursively. In the
second example $_ is interpolated as the literal text it contains. Perl does
not attempt a 'second round' of interpolation on this.


 
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
Is the ability to read Hex data like learning to read the matrix? kemp MCSE 2 10-13-2006 03:58 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
hex value in string back to real hex value jack Python 4 09-08-2004 07:11 AM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



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