473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

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 51460
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: chandy | last post by:
Hi, I have an Html document that declares that it uses the utf-8 character set. As this document is editable via a web interface I need to make sure than high-ascii characters that may be...
4
by: wob | last post by:
Many thanks for those who responded to my question of "putting greek char into C string". In searching for an solution, I noticed that there are more than one version of "Extended ASCII...
2
by: Martín Marconcini | last post by:
Hello there, I'm writting (or trying to) a Console Application in C#. I has to be console. I remember back in the old days of Cobol (Unisys), Clipper and even Basic, I used to use a program...
18
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found...
31
by: Claude Yih | last post by:
Hi, everyone. I got a question. How can I identify whether a file is a binary file or an ascii text file? For instance, I wrote a piece of code and saved as "Test.c". I knew it was an ascii text...
24
by: ChaosKCW | last post by:
Hi I am reading from an oracle database using cx_Oracle. I am writing to a SQLite database using apsw. The oracle database is returning utf-8 characters for euopean item names, ie special...
7
by: Jeffrey Spoon | last post by:
Hello, I'm a bit stuck trying to convert a text file which contains extended ASCII text and changing the ASCII values so they become readable. I do this by subtracting 127 from the ASCII value....
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
4
by: meendar | last post by:
Hi, I am having a character pointer which contains ascii values. i just want to convert all these ascii values to respective characters and again store it in another character pointer. ...
9
by: =?Utf-8?B?RGFu?= | last post by:
I have the following code section that I thought would strip out all the non-ascii characters from a string after decoding it. Unfortunately the non-ascii characters are still in the string....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.