ascii to decimal

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mccommunity
Forum Commoner
Posts: 62
Joined: Mon Oct 07, 2002 8:55 am

ascii to decimal

Post 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
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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.
mccommunity
Forum Commoner
Posts: 62
Joined: Mon Oct 07, 2002 8:55 am

This does not seem to be what I am looking for

Post by mccommunity »

This does not seem to work. It returns a 0 value every time I input a p and it should return 112
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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?
Post Reply