他の人が述べたように、これはばかげた要件です。正常なシステムであれば、透過的な 1:1 マッピングを使用するだけです。でも、言われたことはやらないといけないと思うので…
これを拡張して、65535 個の ID を考慮してみましょう。これは unsigned short の上限であり、整数として格納するには 2 バイト (16 ビット) が必要です。目的の出力は 7 バイトであるため、いくつかのビットを移動することで、元の入力番号から直接派生した一意の可逆 ID を生成できます。これは一見、入力とは関係ありません。
しかし... ASCII 表現を数値にする必要があります。問題ありません - 数値を 3 ビットのチャンクにエンコードし、それを 0x30 で OR するだけです - これは、エンコードされたすべてのバイトが 0 ~ 7 の ASCII コード ポイントを持つことを意味します。
それを理解したら、あとはシステムを選択するだけです。簡単にするために、ビット 1 ~ 16 をステップ実行し、それらを 7 つの出力バイトに均等に分散させます。これはまだかなり予測可能なものを生成します-特にローエンドでは多くのゼロが含まれているため、結果を既知のキーでXORすることで少しスパイスを加えます.
<?php
// Produces a key of the supplied length
// This will always produce the same result, it just alternates
// the least significant 3 bits of every output byte
function generate_xor_key($length)
{
$result = array_fill(0, $length, 0);
for ($i = 0, $bit = 1; $i < $length; $i++) {
for ($j = 0; $j < 3; $j++, $bit++) {
$result[$i] |= ($bit % 2) << $j;
}
}
return implode('', array_map('chr', $result));
}
// Encode an ID
// If using a custom key this can be supplied in the 4th argument
// Keys must always be strings with all the bytes in the range 0x00 - 0x08
function encode_id($id, $encodedLength = 7, $rawBits = 16, $key = null)
{
// Because we are encoding the number into the least significant 3 bits,
// it doesn't make sense for $rawBits > $encodedLength * 3
$maxRawBits = $encodedLength * 3;
if ($rawBits > $maxRawBits) {
trigger_error('encode_id(): $rawBits must be no more than 3 times greater than $encodedLength');
return false;
}
// Get a usable key
if ($key === null) {
$key = generate_xor_key($encodedLength);
}
// Start with all bytes at ASCII 0
$result = array_fill(0, $encodedLength, 0x30);
// Extract each relevant bit from the input and store it in the output bytes
for ($position = 0; $position < $rawBits; $position++) {
$bit = (($id >> $position) & 0x01) << floor($position / $encodedLength);
$index = $position % $encodedLength;
$result[$index] |= $bit;
}
// Pad the remaining bits with alternation
// This is purely cosmetic for the output
for (; $position < $maxRawBits; $position++) {
$index = $position % $encodedLength;
$bit = ($position % 2) << floor($position / $encodedLength);
$result[$index] |= $bit;
}
// Convert the result to an ascii string
return implode('', array_map('chr', $result)) ^ $key;
}
function decode_id($id, $encodedLength = 7, $rawBits = 16, $key = null)
{
// Get a usable key
if ($key === null) {
$key = generate_xor_key($encodedLength);
}
// Convert the string to our original bytes array
$bytes = array_map(
'ord',
str_split(
str_pad($id, $encodedLength, '0', STR_PAD_LEFT) ^ $key,
1
)
);
$result = 0;
// Put the number back together
for ($position = 0; $position < $rawBits; $position++) {
$index = $position % $encodedLength;
$bit = (($bytes[$index] >> floor($position / $encodedLength)) & 0x01) << $position;
$result |= $bit;
}
return $result;
}
http://codepad.org/hfZ4YBKI
連続するすべての ID は、以前の ID と非常に似ています (通常は 1 桁のみが変更されています)。
上記で実装したように、このメカニズムは実際には 21 ビットのエントロピーに対応できるため、2097152 個の一意の ID (ゼロを含む) を生成できます。