Connecting Tech Pros Worldwide Forums | Help | Site Map

ascii to hex

kuukelekuu@gmail.com
Guest
 
Posts: n/a
#1: Aug 4 '06
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


Erwin Moller
Guest
 
Posts: n/a
#2: Aug 4 '06

re: ascii to hex


kuukelekuu@gmail.com wrote:
Quote:
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
adlerweb
Guest
 
Posts: n/a
#3: Aug 4 '06

re: ascii to hex


kuukelekuu@gmail.com wrote:
Quote:
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
Kimmo Laine
Guest
 
Posts: n/a
#4: Aug 4 '06

re: ascii to hex


<kuukelekuu@gmail.comwrote in message
news:1154675584.599984.45480@m79g2000cwm.googlegro ups.com...
Quote:
>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
spam@outolempi.net || Gedoon-S @ IRCnet || rot13(xvzzb@bhgbyrzcv.arg)


kuukelekuu@gmail.com
Guest
 
Posts: n/a
#5: Aug 4 '06

re: ascii to hex


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:
Quote:
<kuukelekuu@gmail.comwrote in message
news:1154675584.599984.45480@m79g2000cwm.googlegro ups.com...
Quote:
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
spam@outolempi.net || Gedoon-S @ IRCnet || rot13(xvzzb@bhgbyrzcv.arg)
Kimmo Laine
Guest
 
Posts: n/a
#6: Aug 4 '06

re: ascii to hex


<kuukelekuu@gmail.comwrote in message
news:1154686960.096434.207540@b28g2000cwb.googlegr oups.com...
Quote:
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
spam@outolempi.net || Gedoon-S @ IRCnet || rot13(xvzzb@bhgbyrzcv.arg)


Closed Thread