dump the content of a string

T

toylet

my $line=<>;

I input 'a' and press enter.

$line should now be 2 bytes in length.

1. How could I access the 2 bytes respectively
2. and print them in hex format?
 
W

Web Surfer

[This followup was posted to comp.lang.perl.misc]

toylet said:
my $line=<>;

I input 'a' and press enter.

$line should now be 2 bytes in length.

1. How could I access the 2 bytes respectively

You can use "substr" to extract portions of strings

2. and print them in hex format?


printf "%02x\n",$data; # print data in hex
 
J

Jürgen Exner

toylet said:
my $line=<>;

I input 'a' and press enter.

$line should now be 2 bytes in length.

Not necessarily. It depends on your OS.
If you want to write portable code don't expect the input to be just 2
characters.
1. How could I access the 2 bytes respectively

perldoc -f substr
2. and print them in hex format?

perldoc -f ord
perldoc -f hex

jue
 
U

Uri Guttman

JE> perldoc -f ord
JE> perldoc -f hex

hex converts from ascii hex to binary. it doesn't print hex.

sprintf or unpack are two ways to do it.

uri
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top