?
Solved

integer to string convertion function

Posted on 2006-05-21
2
Medium Priority
?
4,188 Views
Last Modified: 2011-08-18
hey, i need to convert integer to string
like
"1745" to "agde"

and then to int again...

 "agde"  to "1745"...
0
Comment
Question by:svsvsv
2 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 16728056
Hi,

You can use this function:

<?php
 function swap($in) {
    $tokens = "abcdefghij1234567890";
    $out = '';
    for($i = 0, $c = strlen($in); $i < $c; $i++) {
      $out .= substr($tokens, (strpos($tokens, substr($in,0,1)) + 10) % 20, 1);
      $in = substr($in,1);
    }
    return $out;
 }
 echo swap('1745').'<br/>';
 echo swap('agde');
?>

It does not take into account characters above "j" or non-alphanumeric characters.

-r-
0
 
LVL 37

Accepted Solution

by:
Harisha M G earned 2000 total points
ID: 16728063
Hi,

<?php

function int2str($x)
{
    $x = sprintf("%s",$x);
    for($i=0;$i<strlen($x);$i++)
    {
        $y .= chr($x[$i]+96);
    }
    return $y;
}

function str2int($x)
{
    $y = "";
    for($i=0;$i<strlen($x);$i++)
    {
        $y .= sprintf("%d",ord($x[$i])-96);
    }
    return $y;
}

echo int2str("1732");
echo str2int("abcd");

?>

---
Harish
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question