Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

ASCII code of a char

FatalsniperFatalsniper Posts: 55Member
Hi there,

Does anybody knows how do I get the ASCII value of a character?

Thanks a lot

Fatalsniper

Comments

  • XfactorXfactor Posts: 343Member
    : Hi there,
    :
    : Does anybody knows how do I get the ASCII value of a character?
    :
    : Thanks a lot
    :
    : Fatalsniper

    I think what you need is the pack function.
    The syntax is:
    $return_str = pack (TEMPLATE, LIST);
    LIST is an array of whatever, in your case, an array of characters.
    TEMPLATE tells how the LIST is converted. So there are a bunch of options that can go here. The one you want is 'a' ASCII null padded and 'A', ASCII space padded. If you have 4 elements in LIST, you'd have to specify "A4" OR "AAAA".
    If this isn't clear, I'm sure someone else can help you or you should be able to find this online somewhere.

  • JonathanJonathan Posts: 2,914Member
    : Does anybody knows how do I get the ASCII value of a character?
    Use the ord and chr functions.

    print chr(50); # Prints 2, which has ASCII character code 50
    print ord('2'); # Prints 50, which is the ASCII character code for 2

    Hope this helps,

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • FatalsniperFatalsniper Posts: 55Member
    : : Does anybody knows how do I get the ASCII value of a character?
    : Use the ord and chr functions.
    :
    : print chr(50); # Prints 2, which has ASCII character code 50
    : print ord('2'); # Prints 50, which is the ASCII character code for 2
    :
    : Hope this helps,
    :
    : Jonathan
    :
    : ###
    : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
    :
    :
    THANKS FOR YOUR HELP
  • XfactorXfactor Posts: 343Member
    : print chr(50); # Prints 2, which has ASCII character code 50
    : print ord('2'); # Prints 50, which is the ASCII character code for 2
    :
    : Hope this helps,
    :
    : Jonathan
    This is a way better method than the one I gave. I had forgot about these functions.

  • FatalsniperFatalsniper Posts: 55Member
    : : print chr(50); # Prints 2, which has ASCII character code 50
    : : print ord('2'); # Prints 50, which is the ASCII character code for 2
    : :
    : : Hope this helps,
    : :
    : : Jonathan
    : This is a way better method than the one I gave. I had forgot about these functions.
    :
    :
    Thanks a lot man, I needed that, 'cause I'm working on a program that uses a random access file, and I needed that function to convert the characters of the Primary key to apply a hashing function on them, I'm new in perl but it's very similar to C or pascal in many ways.

    Thank you
  • mdw1982mdw1982 Posts: 124Member
    : : print chr(50); # Prints 2, which has ASCII character code 50
    : : print ord('2'); # Prints 50, which is the ASCII character code for 2
    : :
    : : Hope this helps,
    : :
    : : Jonathan
    : This is a way better method than the one I gave. I had forgot about these functions.
    :
    :

    Ok...so how does one convert from normal text to its hex equivilant?

    i.e. 'a' => ?

    Mark
  • JonathanJonathan Posts: 2,914Member
    : Ok...so how does one convert from normal text to its hex equivilant?
    :
    : i.e. 'a' => ?
    Can't find a built in function that does it, so easiest way seems to be to use sprintf and ord together:-

    $hex = sprintf('%x', ord('a'));

    Or just:-

    $hex = sprintf '%x', ord 'a';

    If you want to use it often you can to something like:-

    sub chartohex { sprintf '%x', ord shift }

    Then you just have to write:-

    $hex = chartohex('a');

    Would be happy to hear neater solutions,

    Jonathan



    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.