Page 1 of 1

string to int (ASCII) conversion problem

Posted: Wed Dec 01, 2010 8:22 am
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

Re: string to int (ASCII) conversion problem

Posted: Wed Dec 01, 2010 8:41 am
by Darhazer

Code: Select all

<?php
$string = '097110107';
$parts = str_split($string, 3);
$result = '';
foreach ($parts as $part) {
	$result .= chr($part);
}
echo $result;
?>

Re: string to int (ASCII) conversion problem

Posted: Wed Dec 01, 2010 10:48 am
by AbraCadaver
For fun:

Code: Select all

$old = '097110107';
$new = implode(array_map('chr', str_split($old, 3)));