469,270 Members | 1,117 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

ascii to hex

I need to convert ascii chars to hex chars.

I searched the internet, found hex to ascii, but nowhere is there a
ascii to hex method created by anyone.

Can anyone help me with that?

Chears

Aug 4 '06 #1
5 50440
ku********@gmail.com wrote:
I need to convert ascii chars to hex chars.

I searched the internet, found hex to ascii, but nowhere is there a
ascii to hex method created by anyone.

Can anyone help me with that?

Chears
Hi,

You question is a bit confusing. :-)
hex is nothing more than a 16-base notation.
ascii is definition to declare the first 128 values of a byte to characters
(or more depending on which one you pick).

So that are two different animals.

In PHP:

int -ascii char : chr()
http://nl2.php.net/manual/en/function.chr.php

ascii -int : ord()
http://nl2.php.net/manual/en/function.ord.php

Note that the int is 10-base, so you'll have to transform it into hex by
using dexhex() and hexdec() depending on the direction. :-)

Also, this may be of use:
http://www.lookuptables.com/

Hope that helps you going.

Regards,
Erwin Moller
Aug 4 '06 #2
ku********@gmail.com wrote:
I need to convert ascii chars to hex chars.

I searched the internet, found hex to ascii, but nowhere is there a
ascii to hex method created by anyone.

Can anyone help me with that?

Chears
I created such a method some time ago - maybe it can help you...

function ascii2hex($ascii) {
$hex = '';
for ($i = 0; $i < strlen($ascii); $i++) {
$byte = strtoupper(dechex(ord($ascii{$i})));
$byte = str_repeat('0', 2 - strlen($byte)).$byte;
$hex.=$byte." ";
}
return $hex;
}

function hex2ascii($hex){
$ascii='';
$hex=str_replace(" ", "", $hex);
for($i=0; $i<strlen($hex); $i=$i+2) {
$ascii.=chr(hexdec(substr($hex, $i, 2)));
}
return($ascii);
}

adlerweb
Aug 4 '06 #3
<ku********@gmail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
>I need to convert ascii chars to hex chars.

I searched the internet, found hex to ascii, but nowhere is there a
ascii to hex method created by anyone.

$hex = "64";
$ascii = "a";
echo chr(hexdec($hex));
echo dechex(ord($ascii));

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net || Gedoon-S @ IRCnet || rot13(xv***@bhgbyrzcv.arg)
Aug 4 '06 #4
Thanks all,

I found it thanks to these posts:

$len = strlen('The lenght of the text is 28');

$a = round(($len / 16));
$b = round(($len - 16) * $a);

echo chr(dechex($b));
Kimmo Laine wrote:
<ku********@gmail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
I need to convert ascii chars to hex chars.

I searched the internet, found hex to ascii, but nowhere is there a
ascii to hex method created by anyone.


$hex = "64";
$ascii = "a";
echo chr(hexdec($hex));
echo dechex(ord($ascii));

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net || Gedoon-S @ IRCnet || rot13(xv***@bhgbyrzcv.arg)
Aug 4 '06 #5
<ku********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Thanks all,

I found it thanks to these posts:

$len = strlen('The lenght of the text is 28');

$a = round(($len / 16));
$b = round(($len - 16) * $a);

echo chr(dechex($b));

That makes no sense to me. But I'm not gonna say anything, if you feel you
got it right and you think it somehow works (does what you want to do) then
it's okay. But it does not, however, convert a hex to an ascii. instead it
converts "something" (I have absolutely no idea what the string length and a
few mathematical operations are supposed to produce) from decimal number to
hex and that to ascii which is totally not what you asked. But perhpas this
is what you want, I dunno...

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net || Gedoon-S @ IRCnet || rot13(xv***@bhgbyrzcv.arg)
Aug 4 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

37 posts views Thread by chandy | last post: by
31 posts views Thread by Claude Yih | last post: by
24 posts views Thread by ChaosKCW | last post: by
7 posts views Thread by Jeffrey Spoon | last post: by
399 posts views Thread by =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post: by
4 posts views Thread by meendar | last post: by
9 posts views Thread by =?Utf-8?B?RGFu?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.