By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,852 Members | 2,220 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,852 IT Pros & Developers. It's quick & easy.

Help reg printing non printable hex characters

P: 4
Hi All,

i am trying to write a code which will take a non printable character as an input and used as a separator for different fields

ex : if user gives an input \xd1 the o/p should be field1 ╤ field2

initially i have written a code to get the below lines in my program hex.pl

Expand|Select|Wrap|Line Numbers
  1.  
  2. use strict;
  3.  
  4. my $seperatorArg;
  5.  
  6. $seperatorArg = shift(@ARGV);
  7.  
  8. print "field1 $seperatorArg field2";
  9.  
  10.  
when i ran "perl hex.pl \xd1" i got the below o/p

field1 \xd1 field2

if i hardcode the value in my code like
Expand|Select|Wrap|Line Numbers
  1. $seperatorArg = "\xd1";
  2.  
i am getting the desired.

can anybody explain my below understanding is correct?

what i am thinking is when i hard code, the value i gave in double quotes and hence the variable interpoplation is occuring which results in hex character

when i gave the same value as input in command line the value is getting in single quotes which is not getting interpolated and hence the same value is being printed.

please point me to the right direction if i am in wrong direction, so tht i can put more effort to make my code to behave in the way i want.

Thanks
Raj
Jun 17 '08 #1
Share this Question
Share on Google+
3 Replies


KevinADC
Expert 2.5K+
P: 4,059
Hi All,

i am trying to write a code which will take a non printable character as an input and used as a separator for different fields

ex : if user gives an input \xd1 the o/p should be field1 ╤ field2

initially i have written a code to get the below lines in my program hex.pl

Expand|Select|Wrap|Line Numbers
  1.  
  2. use strict;
  3.  
  4. my $seperatorArg;
  5.  
  6. $seperatorArg = shift(@ARGV);
  7.  
  8. print "field1 $seperatorArg field2";
  9.  
  10.  
when i ran "perl hex.pl \xd1" i got the below o/p

field1 \xd1 field2

if i hardcode the value in my code like
Expand|Select|Wrap|Line Numbers
  1. $seperatorArg = "\xd1";
  2.  
i am getting the desired.

can anybody explain my below understanding is correct?

what i am thinking is when i hard code, the value i gave in double quotes and hence the variable interpoplation is occuring which results in hex character

when i gave the same value as input in command line the value is getting in single quotes which is not getting interpolated and hence the same value is being printed.

please point me to the right direction if i am in wrong direction, so tht i can put more effort to make my code to behave in the way i want.

Thanks
Raj
You seem to have determined what the problem is. \x is not getting interpolated. Unfortunately I don't know how you can accomplish what you want. Probably pack/unpack/sprintf but I don't know how. Ask on www.perlmonks.com if you don't get an answer here.
Jun 17 '08 #2

P: 4
Hi Kevin

Thanks for your pointers

i tried in this way

Expand|Select|Wrap|Line Numbers
  1. use constant HEXD1 => "\xd1";
  2. use constant HEXDC => "\xdc";
  3.  
  4.  
  5. $arg = shift(@ARGV);
  6.  
  7. if ($arg eq "HEXD1") {
  8.     $arg = HEXD1;
  9.     print " HEX D1 IS $arg \n";
  10. }
  11. elsif ($arg eq "HEXDC") {
  12.                 $arg = HEXDC;
  13.     print " HEX DC IS $arg \n";
  14. }
  15.  
i am running the program as below

perl hex.pl HEXD1

perl hex.pl HEXDC

i know this is a bad way of coding.... :(

trying in different ways.. but couldnt get the expected o/p.

may i get any other pointers

-Raj
Jun 18 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
A hash would easier to use but I think if you ask on perlmonks someone there will give you a good suggestion.
Jun 18 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.