dump the real string

T

toylet

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?
 
A

Andrew McGregor

toylet said:
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
 
T

toylet

my $line = said:
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().
 
T

toylet

my $line = said:
perldoc -f substr
perldoc -f hex

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

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

toylet

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

toylet

toylet said:
I tried this:
printf(".%x.\n.%x.\n",substr($line,0,1),substr($line,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(substr($line,1,1)));
 
G

Glenn Jackman

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

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

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

Jürgen Exner

toylet said:
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
 
J

Jürgen Exner

toylet said:
OK, I think it should be length().

That's wrong. length() will return the number of characters in the string.
This is totally different from the length of the string in bytes.

If you want the length in bytes then probably you will have to dig very deep
into the perl implementation. But I wonder why you would want to know this
implementation detail?

jue
 
G

gnari

toylet said:
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?

take a look at unpack() and his brother pack()

gnari
 
J

Jay Tilton

toylet <toylet_at_mail.hongkong.com> wrote:

: toylet wrote:
: >>> my $line = <>;
: >> perldoc -f substr
: >> perldoc -f hex
: >
: > I tried this:
: > printf(".%x.\n.%x.\n",substr($line,0,1),substr($line,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(substr($line,1,1)));

Yuck.

printf "%vx\n", $line;
 
T

toylet

That's wrong. length() will return the number of characters in the string.
This is totally different from the length of the string in bytes.

You emant each char in a perl string is not stored as one byte?
If you want the length in bytes then probably you will have to dig very deep
into the perl implementation. But I wonder why you would want to know this
implementation detail?

Sorry, I possibly asked the wrong question. I don't really need that deep.
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top