I just started learning Perl, and I've used your question as an exercise. :) Suppose the data file looks like: And output (to STDOUT) would be: // This is data // This is data 10 0xa 20 0x14 12 0xc 34 0x22 123 0x7b Here is my code, may not be good, but it works: #! /usr/local/bi
nice try, but probably you need s/\$&/\$1/; a shorter way is: #!perl -lp $_=sprintf("0x%x",$_);
【在 r*****o 的大作中提到】 : I just started learning Perl, and I've used your question as an exercise. :) : Suppose the data file looks like: And output (to STDOUT) would be: : // This is data // This is data : 10 0xa : 20 0x14 : 12 0xc : 34 0x22 : 123 0x7b : Here is my code, may not be good, but it works: : #! /usr/local/bi
r*o
6 楼
Yeah, you are right, should have used $1 instead of $& And I didn't know the -lp way, it's much more convenient. What if I want some leading zeroes in the hex output, like: 0x000a, 0x0014 Is there any easy way to do that? Thanks.
【在 p******f 的大作中提到】 : nice try, but probably you need s/\$&/\$1/; : a shorter way is: : #!perl -lp : $_=sprintf("0x%x",$_);
p*f
7 楼
"0x%04x" for sprintf perldoc -f sprintf
【在 r*****o 的大作中提到】 : Yeah, you are right, should have used $1 instead of $& : And I didn't know the -lp way, it's much more convenient. : What if I want some leading zeroes in the hex output, like: : 0x000a, 0x0014 : Is there any easy way to do that? : Thanks.