473,503 Members | 1,834 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: 000000000040900000 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 6703
.oO(Krakatioison)
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: 000000000040900000 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('000000000040900000', 10, 36); // ouput: ocmn4
print base_convert('ocmn4', 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.net> wrote in message
news:gn********************************@4ax.com...
.oO(Krakatioison)
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: 000000000040900000 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('000000000040900000', 10, 36); // ouput: ocmn4
print base_convert('ocmn4', 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.net> wrote in message
news:gn********************************@4ax.com...
.oO(Krakatioison)
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: 000000000040900000 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('000000000040900000', 10, 36); // ouput: ocmn4
print base_convert('ocmn4', 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= '1000000000040900000';

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

$encoded = base_convert($str, 10, 36); //

echo "Encoded string:".$encoded."<br>";

$decoded = base_convert($encoded, 36, 10);

echo "Decoded string:".$decoded."<br>";

?>

Result from the screen looks like this:

Original string: 1000000000040900000
Encoded string: 7lieey0lh7k0
Decoded string: 1000000000040900008

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****@sssbbbccc.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(Krakatioison)
Result from the screen looks like this:

Original string: 1000000000040900000
Encoded string: 7lieey0lh7k0
Decoded string: 1000000000040900008

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_convert() 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(Krakatioison)
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($alphabet);
$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($alphabet);
$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($string) {
return preg_split('##', $string, -1, PREG_SPLIT_NO_EMPTY);
}
Test results:

Alphabet: 01
Original string: 1000000000040900000
Encoded string: 11011110000010110110101100111010100111010100000101 0110100000
Decoded string: 1000000000040900000

Alphabet: 0123456789ABCDEF
Original string: 1000000000040900000
Encoded string: DE0B6B3A9D415A0
Decoded string: 1000000000040900000

Alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
Original string: 1000000000040900000
Encoded string: 7LIEEY0LH7KW
Decoded string: 1000000000040900000

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= '1000000000040900000';

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

$encoded = base_convert($str, 10, 36); //

echo "Encoded string:".$encoded."<br>";

$decoded = base_convert($encoded, 36, 10);

echo "Decoded string:".$decoded."<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($str1, 10, 36);
$encoded .= '-' . base_convert($str2, 10, 36);
echo 'Encoded string: ', $encoded, "\n";

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

/* test data */
$str= '000000000040900000'; test($str);
$str= '000000000040900001'; test($str);
$str= '900000000040900000'; test($str);
$str= '900000000040900001'; test($str);
$str= '123456789012345678'; test($str);
$str= '987654321098765432'; test($str);
$str= '111111111111111111'; test($str);
$str= '555555555555555555'; test($str);
$str= '999999999999999999'; test($str);
$str= '000000000000000000'; test($str);
?>

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

Original string: 000000000040900001 ==> Encoded string: 0-ocmn5
Decoded string: 000000000040900001

Original string: 900000000040900000 ==> Encoded string: evu4g0-ocmn4
Decoded string: 900000000040900000

Original string: 900000000040900001 ==> Encoded string: evu4g0-ocmn5
Decoded string: 900000000040900001

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

Original string: 987654321098765432 ==> Encoded string: gc0uy9-1msvw8
Decoded string: 987654321098765432

Original string: 111111111111111111 ==> Encoded string: 1u5hvr-1u5hvr
Decoded string: 111111111111111111

Original string: 555555555555555555 ==> Encoded string: 96rher-96rher
Decoded string: 555555555555555555

Original string: 999999999999999999 ==> Encoded string: gjdgxr-gjdgxr
Decoded string: 999999999999999999

Original string: 000000000000000000 ==> Encoded string: 0-0
Decoded string: 000000000000000000
--
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
Wow guys... I just came home and all your answers are really helpful.
I'll be using one of the answers and I'll write back which one I decided to
use, just so you can see it running on my site, or write somewhere to your
resume or whatever, that you helped on www.newsbackup.com project.
Thanks a bunch. Really, really appreciated.

Joe
Jul 17 '05 #11
Dear Michael,
thanks a lot for helping me with this code.
I had to do a small changes with leading zeros and added some additional
coding to add these missing zeros.
But it all came to work out just perfectly. I really appreciate your help.
I used alphabet like this:
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN OPQRSTUVWXYZ
You can see it working now : http://www.newsbackup.com/index.php?n=Gnte
Wherever you click on the site now it ends with characters out of mentioned
alphabet, most I've seen was 6 decimal places...which is nice comparing to
starting 18 decimal places.
Great job.
Joe
Jul 17 '05 #12
There is yet another question here:
- when you look at this address (http://www.newsbackup.com/index.php?n=Gnte)
, it has small and capital letters and in some cases numbers as well.

Do you guys think it will be indexed properly by Google?
Can Google see the difference between small and capital letters?

Joe
Jul 17 '05 #13
Krakatioison wrote:
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: 000000000040900000 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


It looks like your number always has a lot of repeating '0's. That makes it
a good candidate for Run Length Encoding (RLE). Pick a character to
introduce a sequence of zeros and add a count of how many to represent. For
example, using 'Z' as the introducer:

000000000040900000 has 10 leading '0's, so we can compress the first 9 to
'Z9'. Now we have Z9040900000. You can also replace the trailing '0's with
'Z5'. Then you wind up with Z90409Z5.

'course you have to write the code ot encode it 8).

--
Wake
Jul 17 '05 #14
>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.
Ok, here's the brute force method:

Make a list of all possible values of "n". (Arrange to keep updating
the list if more can be added). Put these in a database table.
Assign a sequence number to each of them, starting from 0. Look
up the value, replace it with the sequence number, possibly in
base64 to make it even shorter.

When you get the new value of "n", (un-base64 it if necessary),
look it up in the same database table, for a matching sequence
number, then get the corresponding long form. Then use it.
For example: 000000000040900000 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


Gordon L. Burditt
Jul 17 '05 #15
Krakartioison < do**********@gmail.com > wrote:
There is yet another question here:
- when you look at this address
(http://www.newsbackup.com/index.php?n=Gnte) , it has small and capital
letters and in some cases numbers as well. Do you guys think it will be
indexed properly by Google? Can Google see the difference between small
and capital letters? Joe
I don't think Google indexes dynamic pages well anyway (with querystrings).
You'll notice sites like Amazon and eBay use custom DLLs so their "dynamic"
content can have a pseudo-static url such as

<
http://www.amazon.com/exec/obidos/tg...691493-5262550


and even

< http://search.ebay.com/portable-dvd_...categoryZ52473 >

for searches, presumably to allow for better Google indexing.
--
Jeff Evans
Morgan Stanley Investment Management IT
www.jeffevans.us
Jul 17 '05 #16
On Wed, 27 Oct 2004 23:40:14 -0400, Krakartioison wrote:
Do you guys think it will be indexed properly by Google? Can Google see
the difference between small and capital letters?


It can for URLs. I believe it doesn't distinguish case for the page's
body, however, unless one of the search terms is in mixed case.

HTH,
La'ie Techie

Jul 17 '05 #17

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

Similar topics

7
7602
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
7987
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.,...
21
6198
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...
52
1565
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
3
1846
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...
0
1158
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
1777
by: ad | last post by:
I want to hash a givien string to a hashed string. How can I do witth c#?
4
1522
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
2656
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...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7076
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...
0
7323
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...
0
7453
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...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4670
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...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
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 ...
0
377
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...

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.