Jump to content

Convert number to string!

- - - - -

  • Please log in to reply
6 replies to this topic

#1
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
Hello,
I want to make hash which convert a number to string :
sth like:
input output
1 ahywde
2 yewnse
3 ewdsiq

random output!

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
I am unsure if you wish to use existing function, this should provide a non-hexadecimal only hash.
<?php
$nums = array(1, 2, 3, 4, 5);

foreach($nums as $num)
    echo "$num " ,  substr(strtolower(base64_encode(sha1($num))),0,6) . "\n";
1 mzu2yt
2 n2i1mj
3 owu2og
4 ntnlyt
5 mne2mz

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
Does it work for how many number?
I want to change serial number to string but short one.
Note I have 3 field which should give me one hash!
Table strut
sid (Number that I want change it to string)
type (A Number between 20 number)
Name (String)

SN (String)

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
You can simply concatenate the fields and pass it through my sha1 set of functions. It will work for any length string and return a short string out of it.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
Can you show how?
I don't know how to use it!

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
All you need to do is make the query, and return the results, and combine them. A simple example:
function doHash($string) {
    return substr(strtolower(base64_encode(sha1($string))), 0, 6);
}

$hashstr = "";

$query = mysql_query("SELECT (sid, type, Name, SN) FROM strut WHERE (...) ");
while($row = mysql_fetch_assoc($query) {
   $hashstr = doHash($row['sid'] . $row['type'] . $row['Name'] . $row['SN']);
}

echo $hashstr;

Is that what you wanted?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#7
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
yes,
This what I need!
+rep




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users