Forum Moderators: coopster

Message Too Old, No Replies

Trying to convert string to decimal equivalent

         

erikcw

5:47 pm on Oct 29, 2004 (gmt 0)

10+ Year Member



Hi all,

I am trying to convert a string of any given length (can include special characters such as :,/,-,.,etc...) into its decimal equivalent. I've tried all of the php functions I can find, but they all seem to deal with hex or binary.

For example:
h == 104 (it is ok if the function adds the html enties on h)

(here is a table of the decimal equivalents [neurophys.wisc.edu ])

I've tried:
HexDec()
bin2hex()
BinDex()
etc....

Can anyone tell me how to convert this value?

Thanks so much!

dmorison

6:34 pm on Oct 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ord() [uk.php.net] will return the ASCII value of a character. You can use this on every character of your input string; but it is not clear from your question what output you are after.

For example, given the string "HELLO", what output are you looking for?

jatar_k

6:38 pm on Oct 29, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what about something like this

<? 
$str = "hello world!";
for($i=0;$i<strlen($str);$i++) {
echo ord($str{$i}) . " ";
}
?>

just added the space after to seperate the chars, otherwise it just looks like a big mess.

dmorison

6:39 pm on Oct 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



By the way, if your objective here is to make a string "web safe", don't forget htmlentities() [uk.php.net].

erikcw

7:10 pm on Oct 29, 2004 (gmt 0)

10+ Year Member



Thanks all for your help!

jatar_k - your code block did exactly the trick! Thanks!

Erik