Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   dump the real string (http://www.velocityreviews.com/forums/t885433-dump-the-real-string.html)

toylet 02-25-2004 01:03 PM

dump the real string
 

my $line = <>;

I input "a" and press enter.

Now $line should contain 2 bytes: 'a' and end-of-line character.

How could I print the content of $line (both bytes) in hex format?


--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 8:58pm up 2:41 1 user 0.99 0.98

Andrew McGregor 02-25-2004 01:23 PM

Re: dump the real string
 

"toylet" <toylet_at_mail.hongkong.com> wrote in message
news:403c9d1f_2@rain.i-cable.com...
>
> my $line = <>;
>
> I input "a" and press enter.
>
> Now $line should contain 2 bytes: 'a' and end-of-line character.
>
> How could I print the content of $line (both bytes) in hex format?
>


perldoc -f substr
perldoc -f hex



toylet 02-25-2004 01:33 PM

Re: dump the real string
 
>> my $line = <>;
>>
>> I input "a" and press enter.
>>
>> Now $line should contain 2 bytes: 'a' and end-of-line character.
>>
>> How could I print the content of $line (both bytes) in hex format?

> perldoc -f substr
> perldoc -f hex


hex() should not be relevant as I need to convert from numeric to hex
digits. Already knew about substr().




--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 9:30pm up 3:13 1 user 1.03 1.00

toylet 02-25-2004 01:35 PM

Re: dump the real string
 
>> my $line = <>;
> perldoc -f substr
> perldoc -f hex


I tried this:
printf(".%x.\n.%x.\n",substr($line,0,1),substr($li ne,1,1));

and perl said substr($line) are not numeric.

--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 9:34pm up 3:17 1 user 1.11 1.04

toylet 02-25-2004 01:39 PM

Re: dump the real string
 
>> my $line = <>;
>> I input "a" and press enter.


and which function will return the length of $line as 2 bytes?
$line is already in scalar context.

--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 9:38pm up 3:21 1 user 1.04 1.01

toylet 02-25-2004 01:42 PM

Re: dump the real string
 
toylet wrote:
>>> my $line = <>;

>> perldoc -f substr
>> perldoc -f hex

>
> I tried this:
> printf(".%x.\n.%x.\n",substr($line,0,1),substr($li ne,1,1));
>
> and perl said substr($line) are not numeric.
>

got it... they got the c and foxpro function chr() from the doc, I saw
ord().

my $line=<>
printf(".%x.\n.%x.\n",ord(substr($line,0,1)),ord(s ubstr($line,1,1)));


--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 9:40pm up 3:23 1 user 1.04 1.01

toylet 02-25-2004 01:42 PM

Re: dump the real string
 
toylet wrote:

>>> my $line = <>;
>>> I input "a" and press enter.

>
> and which function will return the length of $line as 2 bytes?
> $line is already in scalar context.
>

OK, I think it should be length().

--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 9:42pm up 3:25 1 user 1.05 1.01

toylet 02-25-2004 02:08 PM

Re: dump the real string
 
>> Already knew about substr().
> Then why did you ask?


Not sure whether perl store strings like C.


--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 10:08pm up 3:51 1 user 1.13 1.10

Glenn Jackman 02-25-2004 03:11 PM

Re: dump the real string
 
toylet <> wrote:
> my $line=<>
> printf(".%x.\n.%x.\n",ord(substr($line,0,1)),ord(s ubstr($line,1,1)));


Or, not assuming the length of $line is 2:

printf ".%02x.\n", ord foreach (split //, $line);


--
Glenn Jackman
NCF Sysadmin
glennj@ncf.ca

Jürgen Exner 02-25-2004 03:12 PM

Re: dump the real string
 
toylet wrote:
> my $line = <>;
>
> I input "a" and press enter.
>
> Now $line should contain 2 bytes: 'a' and end-of-line character.
>
> How could I print the content of $line (both bytes) in hex format?


Why are you asking the same question twice within 3 minutes?

jue




All times are GMT. The time now is 10:38 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


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