2016-12-16 12 views
0

10個のトグル(1/0)と2つの数字を値として持つ1つの選択入力を持つ非常に単純なHTMLフォームです。フォーム全体からの結果は、000060000111のような12桁の数字(5番目と6番目の数字は選択入力からの数字を表します)として書き込むことができます。フォームの値を短く可読な文字列に圧縮/符号化する

私の目標は、このシーケンスをJavaScriptを使用して、文字列と数字で人間が読める短い文字列に圧縮/エンコードして、フォームの値を参照することです。数字と文字を含む6桁の文字列は完全です(例:An3K7d)。 Ilのようなあいまいな文字は避けてください。

シーケンスをBase64で変換しようとしましたが、出力がさらに長くなりました。

+0

あなたはあいまいさを避けたい場合は、使用することをお勧めします[base32](HTTPS://en.wikipedia .org/wiki/Base32)エンコーディングを使用しています。これはあいまいな文字を避けるために作られています。 – Frxstrem

答えて

1

私はあなたの記述を正しく理解していれば、それを簡単に3文字にすることができます。

可能なトグル設定は1024個あり、2桁の数字は100個ありますので、合計102,400個の値が可能です。そのキューブルートは47未満であるので、47のセットから選択された3つの文字で表すことができます。大文字と小文字だけを使用して、好きでないものを除外することができます(I、l 、o、o、何でも)、まだ十分に残っている。または、そこに数字を投げたり、より少ない文字を使用したりすることができます。あなたが好むものは何でも。

エンコーディングを行うには、入力を0..102,399の範囲の整数に変換するだけです。これは、トグルの10ビットの数値に100を掛けた後、2桁の入力に対して0.99を加えたものに過ぎません。

次に、その整数を47で逐次除算し、剰余を桁として、次の除算に商を使用して、ベース47の整数をエンコードします。デコードするには、乗算してから入力情報を抽出して、基数47桁を再結合します。私は、次のコード(CodePen Demo)と私の問題を解決し、マークの優秀な答えに

+0

あなたの優れた説明をありがとうございました。私はこれに非常に新しいですが、あなたの助けを借りて、私は後でポストするencode/decode関数を書くことができました。 – ohh2ahh

0

ありがとう:

// Base 47 characters 
var chars = [ 
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 
    'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'T', 'U', 
    'V', 'W', 'X', 'Y', 'Z', 'b', 'c', 'd', 'e', 
    'f', 'g', 'h', 'm', 'n', 'p', 'q', 'r', 't', 
    'u', 'v', 'w', 'x', 'y', 'z', '2', '3', '4', 
    '6', '7' 
]; 

function encode(value) { 
    // Get toggle values and convert binary to decimal 
    var toggles = value.slice(0, value.length - 2); // string 
    var decimal = parseInt(toggles, 2); // number (0..1023) 

    // Get two-digit select value 
    var select = parseInt(value.slice(value.length - 2)); // number (0..99) 

    // Combine toggle and select values to a single integer 
    var possibility = (decimal * 100) + select; // number (0..103499) 

    var output = ''; 

    // Get base47 value by successively dividing by 47, 
    // taking the remainder as a digit, and using the quotient 
    // for the next division 
    for(var i = 0; i < 3; i++) { 
    var quotient = Math.floor(possibility/47); 
    var remainder = possibility - (quotient * 47); 
    possibility = quotient; 
    output += chars[remainder]; 
    } 

    return output; 
} // encode(value) 

function decode(value) { 
    var possibility = 0; 

    // Loop through base47 string, beginning from the end 
    // Recombine the base 47 digits by successively multiplying by 47 
    for(var i = value.length - 1; i >= 0; i--) { 
    var item = value[i]; 
    var remainder = chars.indexOf(value[i]); 

    possibility = (possibility * 47) + remainder; 
    } 

    // Convert number to string 
    possibility = possibility.toString(); 

    // Fill numbers < 3 digits with leading zeros 
    while(possibility.length < 3) { possibility = '0' + possibility; } 

    // Get toggles and select values from string 
    var toggles = possibility.slice(0, possibility.length - 2); 
    var select = possibility.slice(possibility.length - 2); 

    // Convert toggles string to binary string and add leading zeros 
    var binary = parseInt(toggles, 10).toString(2); 
    while(binary.length < 10) { binary = '0' + binary; } 

    // Return binary toggle values, followed by the select values 
    return binary + select; 
} // decode(value) 
関連する問題