Help reg printing non printable hex characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • user1357
    New Member
    • Mar 2008
    • 4

    Help reg printing non printable hex characters

    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

    Code:
    use strict;
    
    my $seperatorArg;
    
    $seperatorArg = shift(@ARGV);
    
    print "field1 $seperatorArg field2";
    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
    Code:
    $seperatorArg = "\xd1";
    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
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Originally posted by user1357
    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

    Code:
    use strict;
    
    my $seperatorArg;
    
    $seperatorArg = shift(@ARGV);
    
    print "field1 $seperatorArg field2";
    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
    Code:
    $seperatorArg = "\xd1";
    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.c om if you don't get an answer here.

    Comment

    • user1357
      New Member
      • Mar 2008
      • 4

      #3
      Hi Kevin

      Thanks for your pointers

      i tried in this way

      Code:
      use constant HEXD1 => "\xd1";
      use constant HEXDC => "\xdc";
      
      
      $arg = shift(@ARGV);
      
      if ($arg eq "HEXD1") {
      	$arg = HEXD1;
      	print " HEX D1 IS $arg \n";
      }
      elsif ($arg eq "HEXDC") {
                      $arg = HEXDC;
      	print " HEX DC IS $arg \n";
      }
      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

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        A hash would easier to use but I think if you ask on perlmonks someone there will give you a good suggestion.

        Comment

        Working...