473,738 Members | 10,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to make string of numbers shorter?

My sites navigation is like this:

http://www.newsbackup.com/index.php?...00000040900000

, depending on the variable "n" (which is always a number), it will take me
anywhere on the site... this number is always changing as I have hundreds of
thousand of pages of text on my site.

Problem:
- in my opinion this just not only look weird, but the variable "n" (number)
is too long.

I need a way to somehow express this number in shorter form. Some encoding
and decoding way to express this number as something shorter.
For example: 000000000040900 000 could become something like : AhD7
so the link: http://www.newsbackup.com/index.php?...00000040900000
would look like: http://www.newsbackup.com/index.php?n=AhD7

You know what I mean.

So, I tried to think about it, and I played with the idea to use of gzencode
or gzcompress. Tried it, but these will generate the short code full of
characters out of normal ascii, spaces and so.... I can't use that, then my
site won't be search engine friendly.

Please, do you have any suggestions?
Or some short encoding decoding script? Your script can be forever part of
my site.

Joe
Jul 17 '05 #1
16 6746
.oO(Krakatioiso n)
I need a way to somehow express this number in shorter form. Some encoding
and decoding way to express this number as something shorter.
For example: 000000000040900 000 could become something like : AhD7
so the link: http://www.newsbackup.com/index.php?...00000040900000
would look like: http://www.newsbackup.com/index.php?n=AhD7


If n is a real number (so that leading zeros don't matter), then you
could try base_convert:

print base_convert('0 000000000409000 00', 10, 36); // ouput: ocmn4
print base_convert('o cmn4', 36, 10); // output: 40900000

http://www.php.net/base_convert

HTH
Micha
Jul 17 '05 #2
base64? (without the leading zeroes of course)

"Michael Fesser" <ne*****@gmx.ne t> wrote in message
news:gn******** *************** *********@4ax.c om...
.oO(Krakatioiso n)
I need a way to somehow express this number in shorter form. Some encoding
and decoding way to express this number as something shorter.
For example: 000000000040900 000 could become something like : AhD7
so the link: http://www.newsbackup.com/index.php?...00000040900000
would look like: http://www.newsbackup.com/index.php?n=AhD7


If n is a real number (so that leading zeros don't matter), then you
could try base_convert:

print base_convert('0 000000000409000 00', 10, 36); // ouput: ocmn4
print base_convert('o cmn4', 36, 10); // output: 40900000

http://www.php.net/base_convert

HTH
Micha

Jul 17 '05 #3
Problem is that I need those leading zeros. It will just not work without
them.
Hm... But great Idea. Thanks a lot.
Any better idea? Which would never cut my leading zeros?

Joe


"Joep" <St***@DeStoep. nl> wrote in message
news:41******** *************** @news.xs4all.nl ...
base64? (without the leading zeroes of course)

"Michael Fesser" <ne*****@gmx.ne t> wrote in message
news:gn******** *************** *********@4ax.c om...
.oO(Krakatioiso n)
I need a way to somehow express this number in shorter form. Some
encoding
and decoding way to express this number as something shorter.
For example: 000000000040900 000 could become something like : AhD7
so the link: http://www.newsbackup.com/index.php?...00000040900000
would look like: http://www.newsbackup.com/index.php?n=AhD7


If n is a real number (so that leading zeros don't matter), then you
could try base_convert:

print base_convert('0 000000000409000 00', 10, 36); // ouput: ocmn4
print base_convert('o cmn4', 36, 10); // output: 40900000

http://www.php.net/base_convert

HTH
Micha


Jul 17 '05 #4
So I played with your idea a bit and wrote this script:

<?

$str= '10000000000409 00000';

echo "Original string:".$str." <br>";

$encoded = base_convert($s tr, 10, 36); //

echo "Encoded string:".$encod ed."<br>";

$decoded = base_convert($e ncoded, 36, 10);

echo "Decoded string:".$decod ed."<br>";

?>

Result from the screen looks like this:

Original string: 100000000004090 0000
Encoded string: 7lieey0lh7k0
Decoded string: 100000000004090 0008

My idea was to add number 1 to the from which I would just cut off. But it's
not working... Decoded string has number 8 at the end. Not 0, as original
had.

Geez... what is happening, am I going blind?

Joe
Jul 17 '05 #5
Krakatioison <kl****@sssbbbc cc.com> wrote:
My idea was to add number 1 to the from which I would just cut off.
But it's not working... Decoded string has number 8 at the end. Not
0, as original had.
Geez... what is happening, am I going blind?


The docs for that function (base_convert) mention a loss of precision when
using large numbers, and those are definitely large numbers. That's probably
why the 8 is appearing.

--
eth'nT
Jul 17 '05 #6
.oO(Krakatioiso n)
Result from the screen looks like this:

Original string: 100000000004090 0000
Encoded string: 7lieey0lh7k0
Decoded string: 100000000004090 0008

My idea was to add number 1 to the from which I would just cut off. But it's
not working... Decoded string has number 8 at the end. Not 0, as original
had.

Geez... what is happening, am I going blind?


Nope, it's probably because of the way base_convert() works.
My fault, I should have read the warning on the manual page:

"base_conve rt() may lose precision on large numbers due to properties
related to the internal "double" or "float" type used."

A while ago I wrote a function for calulating permutations, it is also
usable for converting from the decimal system to any other base. I have
to check if it can be adjusted to work with such large numbers ...

Maybe there's another and better way.

Micha
Jul 17 '05 #7
Micha, Ethan,

I thought there must be something like it. But I wasn't sure.
So now I am really puzzled. What would be a best way to deal with this
problem?
I was wondering about using dechex and hexdec.... but that is using
alphabeth only till it gets to F, so as a solution it wouldn't be the best
possible.

Any other ideas?

Joe
Jul 17 '05 #8
.oO(Krakatioiso n)
I thought there must be something like it. But I wasn't sure.
So now I am really puzzled. What would be a best way to deal with this
problem?
I was wondering about using dechex and hexdec.... but that is using
alphabeth only till it gets to F, so as a solution it wouldn't be the best
possible.


A quick 'n dirty hack, using the BCMath extension (should be compiled-in
by default) for converting any (really big) number from base 10 to any
base you like and back:

function encode($number, $alphabet) {
$base = strlen($alphabe t);
$result = '';
while (bccomp($number , 0) == 1) {
$result = $alphabet{bcmod ($number, $base)}.$result ;
$number = bcdiv($number, $base, 0);
}
return $result;
}

function decode($number, $alphabet) {
$base = strlen($alphabe t);
$alphabet = array_flip(str_ split($alphabet ));
$c = strlen($number)-1;
$result = 0;
for ($i = 0; $i <= $c; $i++) {
$temp = bcmul($alphabet[$number{$i}], bcpow($base, $c-$i));
$result = bcadd($result, $temp);
}
return $result;
}
The str_split() function is PHP5, if you don't have it you can use this
one instead:

function str_split($stri ng) {
return preg_split('##' , $string, -1, PREG_SPLIT_NO_E MPTY);
}
Test results:

Alphabet: 01
Original string: 100000000004090 0000
Encoded string: 110111100000101 101101011001110 101001110101000 001010110100000
Decoded string: 100000000004090 0000

Alphabet: 0123456789ABCDE F
Original string: 100000000004090 0000
Encoded string: DE0B6B3A9D415A0
Decoded string: 100000000004090 0000

Alphabet: 0123456789ABCDE FGHIJKLMNOPQRST UVWXYZ
Original string: 100000000004090 0000
Encoded string: 7LIEEY0LH7KW
Decoded string: 100000000004090 0000

With the last alphabet the encoding saves 7 chars ... not that much ...

HTH
Micha
Jul 17 '05 #9
["Followup-To:" header set to comp.lang.php.]
Krakatioison wrote:
So I played with your idea a bit and wrote this script:

<?

$str= '10000000000409 00000';

echo "Original string:".$str." <br>";

$encoded = base_convert($s tr, 10, 36); //

echo "Encoded string:".$encod ed."<br>";

$decoded = base_convert($e ncoded, 36, 10);

echo "Decoded string:".$decod ed."<br>";

?>


Are your numbers 'n' always 18 digits long?

php$ cat longn.php
<?php
function test($str) {
if (strlen($str) != 18) {echo "$str: Oops, wrong length\n\n"; return;}
$str1 = substr($str, 0, 9);
$str2 = substr($str, 9, 9);
echo 'Original string: ', $str, ' ==> ';

$encoded = base_convert($s tr1, 10, 36);
$encoded .= '-' . base_convert($s tr2, 10, 36);
echo 'Encoded string: ', $encoded, "\n";

$enc = explode('-', $encoded);
$decoded = substr('0000000 0' . base_convert($e nc[0], 36, 10), -9);
$decoded .= substr('0000000 0' . base_convert($e nc[1], 36, 10), -9);
echo ' Decoded string: ', $decoded, "\n\n";
}

/* test data */
$str= '00000000004090 0000'; test($str);
$str= '00000000004090 0001'; test($str);
$str= '90000000004090 0000'; test($str);
$str= '90000000004090 0001'; test($str);
$str= '12345678901234 5678'; test($str);
$str= '98765432109876 5432'; test($str);
$str= '11111111111111 1111'; test($str);
$str= '55555555555555 5555'; test($str);
$str= '99999999999999 9999'; test($str);
$str= '00000000000000 0000'; test($str);
?>

php$ php longn.php
Original string: 000000000040900 000 ==> Encoded string: 0-ocmn4
Decoded string: 000000000040900 000

Original string: 000000000040900 001 ==> Encoded string: 0-ocmn5
Decoded string: 000000000040900 001

Original string: 900000000040900 000 ==> Encoded string: evu4g0-ocmn4
Decoded string: 900000000040900 000

Original string: 900000000040900 001 ==> Encoded string: evu4g0-ocmn5
Decoded string: 900000000040900 001

Original string: 123456789012345 678 ==> Encoded string: 21i3v9-7clzi
Decoded string: 123456789012345 678

Original string: 987654321098765 432 ==> Encoded string: gc0uy9-1msvw8
Decoded string: 987654321098765 432

Original string: 111111111111111 111 ==> Encoded string: 1u5hvr-1u5hvr
Decoded string: 111111111111111 111

Original string: 555555555555555 555 ==> Encoded string: 96rher-96rher
Decoded string: 555555555555555 555

Original string: 999999999999999 999 ==> Encoded string: gjdgxr-gjdgxr
Decoded string: 999999999999999 999

Original string: 000000000000000 000 ==> Encoded string: 0-0
Decoded string: 000000000000000 000
--
USENET would be a better place if everybody read: | to mail me: simply |
http://www.catb.org/~esr/faqs/smart-questions.html | "reply" to this post, |
http://www.netmeister.org/news/learn2quote2.html | *NO* MIME, plain text |
http://www.expita.com/nomime.html | and *NO* attachments. |
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
7613
by: lehrig | last post by:
I have a string which is returned by a C extension. mystring = '(1,2,3)' HOW can I read the numbers in python ?
9
8002
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc., etc. The intent is to finish by assigning the list to a string that I would write to disk: recstr = string.join(recd,'')
21
6252
by: AnnMarie | last post by:
<script language="JavaScript" type="text/javascript"> <!-- function validate(theForm) { var validity = true; // assume valid if(frmComments.name.value=='' && validity == true) { alert('Your full name is required. Please enter your full name!'); validity = false; frmComments.name.focus();
52
1565
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? ----------------------------------------------------------------------- When formatting money for example, to format 6.57634 to 6.58, 6.5 to 6.50, and 6 to 6.00? Rounding of x.xx5 is uncertain, as such numbers are not represented exactly.
3
1858
by: skanemupp | last post by:
is there anyway to make this shorter? i hate having these big blocks of similar-looking code, very unaesthetic. maybe doesnt matter good-code-wise? anyway can i make some function that makes this shorter? like put the etiquettes on the button froma string of '123+456-789*0Cr/' ? problem is the command and lambda-func for each button is different. self.btnDisplay = Button(self,text='1',command=lambda
0
1179
by: Frank Birbacher | last post by:
Hi! sphenxes@gmail.com schrieb: Why is the referenceNumber a string? Are there always *exactly* six alternatives? The alternatives could be a std::deque which can resize as needed.
3
1788
by: ad | last post by:
I want to hash a givien string to a hashed string. How can I do witth c#?
4
1542
by: raylopez99 | last post by:
Please consider the below and how to make name references shorter-- there has to be a way. RL using System; namespace MyNamesSpace1 { class ManagerClass
13
2678
by: Tony Johansson | last post by:
Hello! I read in a book and here is a question and the answer that I'm not satisfied with. When should you use the StringBuilder class instead of the String class. 1.When building a string from shorter strings. 2.When working with text data longer than 256 bytes. 3.When you want to search and replace the contents of a string 4.When a string is a value type.
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.