473,836 Members | 1,505 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 6769
.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
7622
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
8008
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
6266
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
1863
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
1182
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
1789
by: ad | last post by:
I want to hash a givien string to a hashed string. How can I do witth c#?
4
1546
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
2687
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
9825
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9671
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
10846
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10551
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...
0
9379
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7793
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5828
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4021
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3116
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.