开发者

How to convert numbers to an alpha numeric system with php

I'm not sure what this is called, which is why I'm having trouble searching for it.

What I'm looking to do is to take numbers and convert them to some alphanumeric base so that the number, say 5000, wo开发者_C百科uldn't read as '5000' but as 'G4u', or something like that. The idea is to save space and also not make it obvious how many records there are in a given system. I'm using php, so if there is something like this built into php even better, but even a name for this method would be helpful at this point.

Again, sorry for not being able to be more clear, I'm just not sure what this is called.


You want to change the base of the number to something other than base 10 (I think you want base 36 as it uses the entire alphabet and numbers 0 - 9).

The inbuilt base_convert function may help, although it does have the limitation it can only convert between bases 2 and 36

$number = '5000';
echo base_convert($number, 10, 36); //3uw


Funnily enough, I asked the exact opposite question yesterday.

The first thing that comes to mind is converting your decimal number into hexadecimal. 5000 would turn into 1388, 10000 into 2710. Will save a few bytes here and there.

You could also use a higher base that utilizes the full alphabet (0-Z instead of 0-F) or even the full 256 ASCII characters. As @Yacoby points out, you can use base_convert() for that.

As I said in the comment, keep in mind that this is not an efficient way to mask IDs. If you have a security problem when people can guess the next or previous ID to a record, this is very poor protection.


dechex will convert a number to hex for you. It won't obfuscate how many records are in a given system, however. I don't think it will make it any more efficient to store or save space, either.

You'd probably want to use a 2 way crypt function if obfuscation is needed. That won't save space, either.

Please state your goals more clearly and give more background, because this seems a bit pointless as it is.


This might confuse more people than simply converting the base of the numbers ...

Try using signed digits to represent your numbers. For example, instead of using digits 0..9 for decimal numbers, use digits -5..5. This Wikipedia article gives an example for the binary representation of numbers, but the approach can be used for any numeric base.

Using this together with, say, base-36 arithmetic might satisfy you.


EDIT: This answer is not really a solution to the question, so ignore it unless you are trying to hash a number.

My first thought we be to hash it using eg. md5 or sha1. (You'd probably not save any space though...)

To prevent people from using rainbow-tables or brute force to guess which number you hashed, you can always add a salt. It can be as simple as a string prepended to your number before hashing it.

md5 would return an alphanumeric string of exactly 32 chars and sha1 would return one of exaclty 40 chars.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜