Ascii to hex?

Discussion in 'Perl' started by Bert, Oct 9, 2003.

  1. Bert

    Bert Guest

    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.
     
    Bert, Oct 9, 2003
    #1
    1. Advertising

  2. Bert

    Purl Gurl Guest

    Purl Gurl, Oct 9, 2003
    #2
    1. Advertising

  3. Bert

    Roy Johnson Guest

    "Bert" <> wrote in message news:<cf4hb.60753$>...
    > How do I convert an ascii to hex?


    /opt/perl/bin/perldoc -q convert
     
    Roy Johnson, Oct 9, 2003
    #3
  4. Bert

    Jim Gibson Guest

    In article <cf4hb.60753$>, Bert
    <> wrote:

    > 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.
    >
    >


    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.
     
    Jim Gibson, Oct 9, 2003
    #4
  5. Bert

    Bert Guest

    Re: Ascii to hex? Thank you

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


    "Bert" <> wrote in message
    news:cf4hb.60753$...
    > 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.
    >
    >
     
    Bert, Oct 13, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,411
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    537
    Juha Autero
    Aug 19, 2003
  3. jack
    Replies:
    4
    Views:
    617
  4. tim

    hex string to hex value

    tim, Nov 22, 2005, in forum: Python
    Replies:
    8
    Views:
    19,143
  5. tim
    Replies:
    2
    Views:
    1,596
    Dennis Lee Bieber
    Nov 23, 2005
Loading...

Share This Page