Loading ...
Sorry, an error occurred while loading the content.

Ascii to hexavalue and decimal

Expand Messages
  • pank_chat245
    Hi everybody, Can somebody throw some light why my code is not working. All I am trying to covert ascii character in to their corresponding hexa value and to
    Message 1 of 6 , Sep 17, 2007
    • 0 Attachment
      Hi everybody,

      Can somebody throw some light why my code is not working.

      All I am trying to covert ascii character in to their corresponding hexa value and to decimal
      values too.

      I am working on Mac OS X

      #!/usr/bin/perl

      $char = ©;
      $hexval = sprintf("%x", $char);
      $decval = hex($hexval);


      print "$char in hex is $hexval and in dec is $decvaln";

      Thanks for suggestiona.

      Pankaj
    • Thiago Nascimento
      Try this code: #!/usr/bin/perl use warnings; use strict; my $char = (c) ; my $decval = ord $char; my $hexval = sprintf( %x , $decval); print $char in hex is
      Message 2 of 6 , Sep 17, 2007
      • 0 Attachment
        Try this code:

        #!/usr/bin/perl

        use warnings;
        use strict;

        my $char = '(c)';
        my $decval = ord $char;
        my $hexval = sprintf("%x", $decval);

        print "$char in hex is $hexval and in dec is $decval";

        HTH.

        On 9/17/07, pank_chat245 <pankaj.chaturvedi@...> wrote:
        >
        > Hi everybody,
        >
        > Can somebody throw some light why my code is not working.
        >
        > All I am trying to covert ascii character in to their corresponding hexa
        > value and to decimal
        > values too.
        >
        > I am working on Mac OS X
        >
        > #!/usr/bin/perl
        >
        > $char = (c);
        > $hexval = sprintf("%x", $char);
        > $decval = hex($hexval);
        >
        > print "$char in hex is $hexval and in dec is $decvaln";
        >
        > Thanks for suggestiona.
        >
        > Pankaj
        >
        >
        >



        --
        Thiago Nascimento
        perl -e '$_="tMM naaCt Feocmama_itpUilucoGa";$_.=$1,print $2 while
        s/(..)(.)//;print substr$_,1,1;'
        "...just because I don't know the meaning of my art, does not mean it has no
        meaning..." S.D.


        [Non-text portions of this message have been removed]
      • Pankaj Chaturvedi
        Perfect!!! I made few changes in it to get © sort of output. Thanks again. ... [Non-text portions of this message have been removed]
        Message 3 of 6 , Sep 17, 2007
        • 0 Attachment
          Perfect!!! I made few changes in it to get © sort of output.

          Thanks again.



          On 9/17/07 5:01 PM, "Thiago Nascimento" <nascimenthiago@...> wrote:

          >
          >
          >
          >
          > Try this code:
          >
          > #!/usr/bin/perl
          >
          > use warnings;
          > use strict;
          >
          > my $char = '(c)';
          > my $decval = ord $char;
          > my $hexval = sprintf("%x", $decval);
          >
          > print "$char in hex is $hexval and in dec is $decval";
          >
          > HTH.
          >
          > On 9/17/07, pank_chat245 <pankaj.chaturvedi@...
          > <mailto:pankaj.chaturvedi%40aptaracorp.com> > wrote:
          >> >
          >> > Hi everybody,
          >> >
          >> > Can somebody throw some light why my code is not working.
          >> >
          >> > All I am trying to covert ascii character in to their corresponding hexa
          >> > value and to decimal
          >> > values too.
          >> >
          >> > I am working on Mac OS X
          >> >
          >> > #!/usr/bin/perl
          >> >
          >> > $char = (c);
          >> > $hexval = sprintf("%x", $char);
          >> > $decval = hex($hexval);
          >> >
          >> > print "$char in hex is $hexval and in dec is $decvaln";
          >> >
          >> > Thanks for suggestiona.
          >> >
          >> > Pankaj
          >> >
          >> >
          >> >




          [Non-text portions of this message have been removed]
        • Pankaj Chaturvedi
          Hi Thiago, Well I¹ve another question related to this, instead of defining $char as Œ(c)¹ or any other special character, can I do something like any symbol
          Message 4 of 6 , Sep 17, 2007
          • 0 Attachment
            Hi Thiago,

            Well I¹ve another question related to this, instead of defining $char as
            Œ(c)¹ or any other special character, can I do something like any symbol or
            special character (as I am not sure which special character can I have in my
            file).

            Let me try to be more clear, all I am trying to search and replace all the
            special characters in an xml to their corresponding Unicode hex value.
            Earlier I was trying to it like:

            !#usr/bin/perl -w
            open (IN, "$ARGV[0]");
            undef $/;
            $_=<IN>;
            $file=$_;

            if ($file=~m/\©/g)
            {
            $file=~s/\©/\ /g;
            }

            elsif ($file=~m/\®/g)

            {
            $file=~s/\®/\®/g;
            }

            open (OUT,">$ARGV[0]");
            print OUT $file;
            close(OUT);

            close(IN);

            But in the above case I need to define all special characters in my if
            condition and then replace it to their hen values, which seems to be too
            lengthy and least desirable.

            I am new in Perl and tried my hands to write a script which I posted
            earlier, but again I do not know how to say/define in perl ³convert any
            special character into its hex value in a file².

            Apologies for asking so much but any suggestions in this regar will be
            highly appreciated.
            Thanks




            On 9/17/07 5:20 PM, "Pankaj Chaturvedi" <pankaj.chaturvedi@...>
            wrote:

            >
            >
            >
            >
            > Perfect!!! I made few changes in it to get © sort of output.
            >
            > Thanks again.
            >
            > On 9/17/07 5:01 PM, "Thiago Nascimento" <nascimenthiago@...
            > <mailto:nascimenthiago%40gmail.com> > wrote:
            >
            >> >
            >> >
            >> >
            >> >
            >> > Try this code:
            >> >
            >> > #!/usr/bin/perl
            >> >
            >> > use warnings;
            >> > use strict;
            >> >
            >> > my $char = '(c)';
            >> > my $decval = ord $char;
            >> > my $hexval = sprintf("%x", $decval);
            >> >
            >> > print "$char in hex is $hexval and in dec is $decval";
            >> >
            >> > HTH.
            >> >
            >> > On 9/17/07, pank_chat245 <pankaj.chaturvedi@...
            >> <mailto:pankaj.chaturvedi%40aptaracorp.com>
            >> > <mailto:pankaj.chaturvedi%40aptaracorp.com> > wrote:
            >>>> >> >
            >>>> >> > Hi everybody,
            >>>> >> >
            >>>> >> > Can somebody throw some light why my code is not working.
            >>>> >> >
            >>>> >> > All I am trying to covert ascii character in to their corresponding
            hexa
            >>>> >> > value and to decimal
            >>>> >> > values too.
            >>>> >> >
            >>>> >> > I am working on Mac OS X
            >>>> >> >
            >>>> >> > #!/usr/bin/perl
            >>>> >> >
            >>>> >> > $char = (c);
            >>>> >> > $hexval = sprintf("%x", $char);
            >>>> >> > $decval = hex($hexval);
            >>>> >> >
            >>>> >> > print "$char in hex is $hexval and in dec is $decvaln";
            >>>> >> >
            >>>> >> > Thanks for suggestiona.
            >>>> >> >
            >>>> >> > Pankaj
            >>>> >> >
            >>>> >> >
            >>>> >> >
            >
            > [Non-text portions of this message have been removed]
            >
            >
            >




            [Non-text portions of this message have been removed]
          • merlyn@stonehenge.com
            ... Thiago my $char = (c) ; Thiago my $decval = ord $char; That s not really decimal . It s just a number internally, without a numeric base. Well,
            Message 5 of 6 , Sep 17, 2007
            • 0 Attachment
              >>>>> "Thiago" == Thiago Nascimento <nascimenthiago@...> writes:

              Thiago> my $char = '(c)';
              Thiago> my $decval = ord $char;

              That's not really "decimal". It's just a "number" internally, without a
              numeric base. Well, technically, it's represented in IEEE Floating Point, but
              you probably don't care about that. The only time it becomes "decimal" is
              when it gets printed, where it is sent automatically through "number to
              decimal" conversions.

              Thiago> my $hexval = sprintf("%x", $decval);

              So this is a "number to hex" conversion, not a decimal to hex conversion.

              Just trying to get the terminology nailed down, because misstating it
              at this level can lead to bizarre conclusions.

              --
              Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
              <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
              Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
              See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
            • Pankaj Chaturvedi
              Hi, Can somebody let me know how to declare a scalar variable as a ³Unicode symbol² say for example $char=¹©¹; I know the following declarations but was
              Message 6 of 6 , Sep 18, 2007
              • 0 Attachment
                Hi,

                Can somebody let me know how to declare a scalar variable as a ³Unicode
                symbol² say for example

                $char=¹©¹;

                I know the following declarations but was not able to find for Unicode
                (ascii) symbols.
                .23E-10 # a very small number
                3.14_15_92 # a very important number
                4_294_967_296 # underscore for legibility
                0xff # hex
                0xdead_beef # more hex
                0377 # octal
                0b011011 # binary

                All I am trying to do is search for unicode symbols in a file and convert
                them into corresponding Hex value.

                Something like below:


                !#usr/bin/perl -w

                open (IN, "$ARGV[0]");
                undef $/;
                $_=<IN>;
                $file=$_;
                $char='©';

                if($file=~m/\$char/g)
                {
                $decval = ord $char;
                sprintf("&#%.5x;", $decval) ;
                }

                open (OUT,">$ARGV[0]");
                print OUT $file;
                close(OUT);
                close(IN);


                I believe that this code is secure one as I am working on an XML document.

                Thanks for help.

                Pankaj




                On 9/17/07 9:10 PM, "merlyn@..." <merlyn@...> wrote:

                >
                >
                >
                >
                >>>>>> >>>>> "Thiago" == Thiago Nascimento <nascimenthiago@...
                >>>>>> <mailto:nascimenthiago%40gmail.com> > writes:
                >
                > Thiago> my $char = '(c)';
                > Thiago> my $decval = ord $char;
                >
                > That's not really "decimal". It's just a "number" internally, without a
                > numeric base. Well, technically, it's represented in IEEE Floating Point, but
                > you probably don't care about that. The only time it becomes "decimal" is
                > when it gets printed, where it is sent automatically through "number to
                > decimal" conversions.
                >
                > Thiago> my $hexval = sprintf("%x", $decval);
                >
                > So this is a "number to hex" conversion, not a decimal to hex conversion.
                >
                > Just trying to get the terminology nailed down, because misstating it
                > at this level can lead to bizarre conclusions.




                [Non-text portions of this message have been removed]
              Your message has been successfully submitted and would be delivered to recipients shortly.