Page 1 of 1

ascii to decimal

Posted: Mon Jan 05, 2004 5:05 pm
by mccommunity
Can someone tell me what command I use to translate ASCII values into decimal values. So it would translate p into 112 and so forth. Thanks.


Mark

Posted: Mon Jan 05, 2004 6:02 pm
by redmonkey
For a single character have a look at ord()

For a complete string check out unpack()

It's actually ASCII character to ASCII decimal.

This does not seem to be what I am looking for

Posted: Mon Jan 05, 2004 9:06 pm
by mccommunity
This does not seem to work. It returns a 0 value every time I input a p and it should return 112

Posted: Mon Jan 05, 2004 9:10 pm
by JAM
Works for me;

Code: Select all

<?php
$o[] = 'p';
$o[] = 'A';
$o[] = 'B';
foreach ($o as $val) {
     echo ord($val) . '<br />';
}
?>
Result:

Code: Select all

112
65
66

Posted: Mon Jan 05, 2004 9:48 pm
by redmonkey
Works for me too.... by pre-pending the &# to the decimal value you get the HTML entity for the character, so here is a little function which demonstrates the unpack() function in use for converting your email address to HTML entities....Although, obviously it will convert any string to it's equivalent HTML entities.

Code: Select all

<?php

function email_obfuscate($address)
{
	$ascii_dec = unpack ('C*', $address);
	$n_chrs = count($ascii_dec);
	$obfus = '';
	$i = 0; while($i++ < $n_chrs) {
		$obfus .= "&#{$ascii_dec[$i]};";
	}
	return $obfus;
}

echo email_obfuscate('somebody_somewhere@nobody-nowhere.com');

?>
Does this qualify for the code snippet section?