Ascii to hex?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bert

    Ascii to hex?

    How do I convert an ascii to hex? I want the hex stored in a variable, not
    just printed out.
    For example,
    my testchars = 'CICS003'
    How do I store that value in hexchars?
    I know this is basic, but it eludes me...
    Thanks from a newbie.


  • Purl Gurl

    #2
    Re: Ascii to hex?

    Bert wrote:

    (snipped)
    [color=blue]
    > How do I convert an ascii to hex?[/color]

    Have you considered research and reading?

    My experience is learning is not painful.
    However, lazy people are a pain.




    Purl Gurl
    --
    Size Does Matter



    #!perl

    @Hex = unpack("C*", "Purl Gurl Rocks");

    print "@Hex\n";

    $ascii = pack("C*", @Hex);

    print $ascii;

    Comment

    • Roy Johnson

      #3
      Re: Ascii to hex?

      "Bert" <grapesoda71@ho tmail.com> wrote in message news:<cf4hb.607 53$k17.27867@bi gnews5.bellsout h.net>...[color=blue]
      > How do I convert an ascii to hex?[/color]

      /opt/perl/bin/perldoc -q convert

      Comment

      • Jim Gibson

        #4
        Re: Ascii to hex?

        In article <cf4hb.60753$k1 7.27867@bignews 5.bellsouth.net >, Bert
        <grapesoda71@ho tmail.com> wrote:
        [color=blue]
        > How do I convert an ascii to hex? I want the hex stored in a variable, not
        > just printed out.
        > For example,
        > my testchars = 'CICS003'
        > How do I store that value in hexchars?
        > I know this is basic, but it eludes me...
        > Thanks from a newbie.
        >
        >[/color]

        You can use ord to get the numerical value of an ascii character and
        sprintf "%x" to generate the hexadecimal representation of that number:

        #!/opt/perl/bin/perl
        use strict;
        use warnings;

        my $testchars = 'CICS003';
        my $hexchars = '';
        foreach my $c (split(//,$testchars)) {
        $hexchars .= sprintf "%x", ord($c);
        }
        print "$hexchars\ n";

        __OUTPUT__
        43494353303033

        FYI: this newsgroup is defunct. Try comp.lang.perl. misc in the future.

        Comment

        • Bert

          #5
          Re: Ascii to hex? Thank you

          Thank you all for your help. A newbie is smarter for your efforts. Thank
          you.


          "Bert" <grapesoda71@ho tmail.com> wrote in message
          news:cf4hb.6075 3$k17.27867@big news5.bellsouth .net...[color=blue]
          > How do I convert an ascii to hex? I want the hex stored in a variable, not
          > just printed out.
          > For example,
          > my testchars = 'CICS003'
          > How do I store that value in hexchars?
          > I know this is basic, but it eludes me...
          > Thanks from a newbie.
          >
          >[/color]


          Comment

          Working...