# ASCII code of a char

Hi there,

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

Thanks a lot

Fatalsniper

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

• : 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.");

• : : 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.");
:
:
• : 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.

• : : 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
• : : 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
• : 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.");