string to int (ASCII) conversion problem

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
softsolutions
Forum Newbie
Posts: 1
Joined: Wed Dec 01, 2010 6:21 am

string to int (ASCII) conversion problem

Post by softsolutions »

Hi,
I am new to php. I am facing an algorithm problem where I have a string consisting of numbers like 097110107.... and this string needs to be broken in groups of 3 digits each such that 097110107 = [097] [110] [107] The length of string is a multiple of 3 of-course for equal length sub-groups and then each sub-group needs to be converted to the ASCII equivalent character like [097] [110] [107] = 'ank' and of-course all sub-groups will be less than 256 value as integer of 3 digits.

How do I do all this in php?

--
Thanks,
Ankit
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: string to int (ASCII) conversion problem

Post by Darhazer »

Code: Select all

<?php
$string = '097110107';
$parts = str_split($string, 3);
$result = '';
foreach ($parts as $part) {
	$result .= chr($part);
}
echo $result;
?>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: string to int (ASCII) conversion problem

Post by AbraCadaver »

For fun:

Code: Select all

$old = '097110107';
$new = implode(array_map('chr', str_split($old, 3)));
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply