Buca Bay - Always nice

Dua tiko noqu toa loaloa, na yacana ko… laga mai…

Base conversion in PHP, Radix 255

September29

Allows you to convert to any base between 2 and 255, effectively using all the ASCII characters.

In order to convert very large numbers with arbitrary precision you’ll need the BCMath lib. Without BCMath the large numbers will not be converted correctly due to PHP not being able to do the arithmetic.

If you need to convert between bases 2-36, you can use the base_convert() function. However, converting to higher bases such as 255 has some benefits, such as “compressing” the characters.

You can successfully compress SHA1 from a 40 byte hex to a 20 byte string.

echo base255(base_convert(sha1('test'), 16, 10)));

Since there is no loss of data, it can be used as a lossless compression. Normal compression such as zlib won’t work on a SHA1 since there are no repeating patterns.

Related posts:

  1. Base conversion in JavaScript I just realized recently that you can convert between number bases in JavaScript using the built in method Object.prototype.toString() and...
  2. Google AJAX Language API with PHP I had noticed some time ago that Google had released an API for their language translation service. A recent forum...
  3. Secure password hashing and storage in PHP Everyone knows you should never store passwords as plain text, right? Recently I’ve come across a lot of bad advice...
posted under php
2 Comments to

“Base conversion in PHP, Radix 255”

  1. On October 21st, 2009 at 8:05 am Julius Beckmann MonsterID Icon Julius Beckmann Says:

    I once got a similar idea for saving hex strings.
    My idea was to split the hex string in parts a 8 chars and convert these parts with base_convert($part, 16, 10) to the decimal value.
    This can be saved in SQL Database with a INT column.

    A SHA1 would be 5*8 chars => 5 INT Columns a 4 Byte => 5*4Byte = 20 Byte.
    Does not change anything about compression but could become handy when generating the INDEX on such a column.
    The normal INDEX on a CHAR column wastes much more space than a INDEX over 4 INT columns.

    Just my 2cent.

    Regards, Julius

  2. On October 21st, 2009 at 12:35 pm bucabay MonsterID Icon bucabay Says:

    That’s a great idea. Most likely faster lookups too.

Email will not be published

Website example

Your Comment:

 
Tag Cloud