const BASE64_KEYS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
const BASE64_VALUES = new Array(123);
for (let i = 0; i < 123; ++i) {
BASE64_VALUES[i] = 64;
}
for (let i = 0; i < 64; ++i) {
BASE64_VALUES[BASE64_KEYS.charCodeAt(i)] = i;
}
const HexChars = '0123456789abcdef'.split('');
let _t = ['', '', '', ''];
let UuidTemplate = _t.concat(_t, '-', _t, '-', _t, '-', _t, '-', _t, _t, _t);
let Indices = UuidTemplate.map(function (x, i) {
return x === '-' ? NaN : i;
}).filter(isFinite);
// fcmR3XADNLgJ1ByKhqcC5Z -> fc991dd7-0033-4b80-9d41-c8a86a702e59
//49sjfkvnVPJJ8AhKbbuZbj
/**
* base64 解码为 uuid
* @param {String} base64
* @returns
* @example
* let test = base64ToUuid('fcmR3XADNLgJ1ByKhqcC5Z')
* console.log(test) //fc991dd7-0033-4b80-9d41-c8a86a702e59
*/
function base64ToUuid(base64) {
if (base64.length !== 22) {
return base64;
}
UuidTemplate[0] = base64[0];
UuidTemplate[1] = base64[1];
for (let i = 2, j = 2; i < 22; i += 2) {
let lhs = BASE64_VALUES[base64.charCodeAt(i)];
let rhs = BASE64_VALUES[base64.charCodeAt(i + 1)];
UuidTemplate[Indices[j++]] = HexChars[lhs >> 2];
UuidTemplate[Indices[j++]] = HexChars[((lhs & 3) << 2) | rhs >> 4];
UuidTemplate[Indices[j++]] = HexChars[rhs & 0xF];
}
return UuidTemplate.join('');
}
let test = base64ToUuid('fcmR3XADNLgJ1ByKhqcC5Z')
console.log(test)
/**
* uuid 解码为 base64
* @param {String} uuid
* @returns
* * @example
* let test = uuidToBase64('fc991dd7-0033-4b80-9d41-c8a86a702e59')
* console.log(test) //fcmR3XADNLgJ1ByKhqcC5Z
*/
function uuidToBase64(uuid) {
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.