Last active
December 24, 2015 15:29
-
-
Save dolpen/6820425 to your computer and use it in GitHub Desktop.
ブーリアン配列とbase64urlを相互変換するやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; | |
| (function () { | |
| var Base64 = function () {}; | |
| Base64.prototype = { | |
| base: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_', | |
| padding: '=', | |
| encodeBooleanList: function (b) { | |
| var t = [], p = 0, i = 0, l = b.length, c; | |
| while (i < b.length || p > 0) { | |
| if (p == 0) c = 0; | |
| c |= ( i < b.length && b[i++]) ? 1 : 0; | |
| if (++p == 6) { | |
| t.push(this.base[c]); | |
| p = 0; | |
| } | |
| c <<= 1; | |
| } | |
| return t.join(''); | |
| }, | |
| decodeBooleanList: function (s) { | |
| var t = [], l = s.length; | |
| for (var i = 0; i < l; i++) { | |
| var c = this.base.indexOf(s.charAt(i)); | |
| for (var p = 5; p >= 0; p--)t.push((c & (1 << p)) > 0); | |
| } | |
| return t; | |
| } | |
| }; | |
| window.base64 = new Base64(); | |
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| usage | |
| base64.encodeBooleanList([true, false, ...]) | |
| 短い | |
| fffttttfttftffttftffffttfttftfttftfttftttfftttfftttfttfttttttttttttffftttfffttfftttttfftftfftftftfffttffftftfffffttftfftttttffttttttftffffftftfftftftt | |
| => HtNDa1uc7f_jjPlKjFBp8_QUr | |
| 再変換時にパディングがかかる場合がある(末尾にfalse) | |
| ffftttf | |
| -> HA | |
| -> ffftttffffff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment