Skip to content

MongoDB UUID、Base64、CSUUID 转换工具

🏷️ MongoDB

html
<div class="form-group"><label for="base64" class="TransformTitle">Base64 :</label> <input type="text" id="base64" class="TransformButton" placeholder="请输入 Base64" /><button id="btnBase64" class="btn btn-primary">转换</button><div class="TransformResult"></div></div>
<div class="form-group"><label for="uuid" class="TransformTitle">UUID :</label> <input type="text" id="uuid" class="TransformButton" placeholder="请输入 UUID" /><button id="btnUuid" class="btn btn-primary">转换</button><div class="TransformResult"></div></div>
<div class="form-group"><label for="juuid" class="TransformTitle">JUUID :</label> <input type="text" id="juuid" class="TransformButton" placeholder="请输入 JUUID" /><button id="btnJuuid" class="btn btn-primary">转换</button><div class="TransformResult"></div></div>
<div class="form-group"><label for="csuuid" class="TransformTitle">CSUUID :</label> <input type="text" id="csuuid" class="TransformButton" placeholder="请输入 CSUUID" /><button id="btnCsuuid" class="btn btn-primary">转换</button><div class="TransformResult"></div></div>
<div class="form-group"><label for="pyuuid" class="TransformTitle">PYUUID :</label> <input type="text" id="pyuuid" class="TransformButton" placeholder="请输入 PYUUID" /><button id="btnPyuuid" class="btn btn-primary">转换</button><div class="TransformResult"></div></div>

<style>
    label.TransformTitle {
        width: 100px;
    }
    input.TransformButton {
        min-width: 300px;
        margin: 0px 10px;
    }
    label.TransformTitle {
        font-weight: bold;
    }
    .TransformResult {
        display: none;
        margin-left: 20px;
    }
    .TransformResult .TransformTo {
        font-style: italic;
    }
    .TransformResult .Type {
        color: #257fad;
    }
    .TransformResult .Str {
        color: #568c3b;
    }
    .TransformResultRow {
        display: block;
    }
</style>

<script>
    $(function() {
        $("#btnBase64").click(function() {
            let val = $("#base64").val();
            var divResult = $($(this).parent()).find(".TransformResult");
            divResult.html(TransformResultHtml(
                { Type: "Base64", Value: toBase64(val) },
                { Type: "UUID", Value: toUUID(val) },
                { Type: "JUUID", Value: toJUUID(val) },
                { Type: "CSUUID", Value: toCSUUID(val) },
                { Type: "PYUUID", Value: toPYUUID(val) }
            )).show();
        });
    });
    $(function() {
        $("#btnUuid").click(function() {
            let val = UUID($("#uuid").val());
            var divResult = $($(this).parent()).find(".TransformResult");
            divResult.html(TransformResultHtml(
                { Type: "Base64", Value: toBase64(val) },
                { Type: "UUID", Value: toUUID(val) },
                { Type: "JUUID", Value: toJUUID(val) },
                { Type: "CSUUID", Value: toCSUUID(val) },
                { Type: "PYUUID", Value: toPYUUID(val) },
                { Type: "HexUUID", Value: toHexUUID(4, val) }
            )).show();
        });
    });
    $(function() {
        $("#btnJuuid").click(function() {
            let val = JUUID($("#juuid").val());
            var divResult = $($(this).parent()).find(".TransformResult");
            divResult.html(TransformResultHtml(
                { Type: "Base64", Value: toBase64(val) },
                { Type: "UUID", Value: toUUID(val) },
                { Type: "JUUID", Value: toJUUID(val) },
                { Type: "CSUUID", Value: toCSUUID(val) },
                { Type: "PYUUID", Value: toPYUUID(val) },
                { Type: "HexUUID", Value: toHexUUID(3, val) }
            )).show();
        });
    });
    $(function() {
        $("#btnCsuuid").click(function() {
            let val = CSUUID($("#csuuid").val());
            var divResult = $($(this).parent()).find(".TransformResult");
            divResult.html(TransformResultHtml(
                { Type: "Base64", Value: toBase64(val) },
                { Type: "UUID", Value: toUUID(val) },
                { Type: "JUUID", Value: toJUUID(val) },
                { Type: "CSUUID", Value: toCSUUID(val) },
                { Type: "PYUUID", Value: toPYUUID(val) },
                { Type: "HexUUID", Value: toHexUUID(3, val) }
            )).show();
        });
    });
    $(function() {
        $("#btnPyuuid").click(function() {
            let val = PYUUID($("#pyuuid").val());
            var divResult = $($(this).parent()).find(".TransformResult");
            divResult.html(TransformResultHtml(
                { Type: "Base64", Value: toBase64(val) },
                { Type: "UUID", Value: toUUID(val) },
                { Type: "JUUID", Value: toJUUID(val) },
                { Type: "CSUUID", Value: toCSUUID(val) },
                { Type: "PYUUID", Value: toPYUUID(val) },
                { Type: "HexUUID", Value: toHexUUID(3, val) }
            )).show();
        });
    });
    function TransformResultHtml(...results) {
        let resultHtml = "";
        for(let result of results) {
            console.log(result);
             resultHtml = resultHtml + "<div class=\"TransformResultRow\"><span class=\"TransformTo\">to</span> <code>" + result.Type + "</code> : <span class=\"TransformToResult\">" + result.Value + "</span></div>";
        }
        console.log(resultHtml);
        return resultHtml;
    }
    function HexToBase64(hex) {
        var base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        var base64 = "";
        var group;
        for (var i = 0; i < 30; i += 6) {
            group = parseInt(hex.substr(i, 6), 16);
            base64 += base64Digits[(group >> 18) & 0x3f];
            base64 += base64Digits[(group >> 12) & 0x3f];
            base64 += base64Digits[(group >> 6) & 0x3f];
            base64 += base64Digits[group & 0x3f];
        }
        group = parseInt(hex.substr(30, 2), 16);
        base64 += base64Digits[(group >> 2) & 0x3f];
        base64 += base64Digits[(group << 4) & 0x3f];
        base64 += "==";
        return base64;
    }
    function Base64ToHex(base64) {
        var base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
        var hexDigits = "0123456789abcdef";
        var hex = "";
        for (var i = 0; i < 24;) {
            var e1 = base64Digits.indexOf(base64[i++]);
            var e2 = base64Digits.indexOf(base64[i++]);
            var e3 = base64Digits.indexOf(base64[i++]);
            var e4 = base64Digits.indexOf(base64[i++]);
            var c1 = (e1 << 2) | (e2 >> 4);
            var c2 = ((e2 & 15) << 4) | (e3 >> 2);
            var c3 = ((e3 & 3) << 6) | e4;
            hex += hexDigits[c1 >> 4];
            hex += hexDigits[c1 & 15];
            if (e3 != 64) {
                hex += hexDigits[c2 >> 4];
                hex += hexDigits[c2 & 15];
            }
            if (e4 != 64) {
                hex += hexDigits[c3 >> 4];
                hex += hexDigits[c3 & 15];
            }
        }
        return hex;
    }
    function UUID(uuid) {
        var hex = uuid.replace(/[{}-]/g, ""); // remove extra characters
        var base64 = HexToBase64(hex);
        return base64; // new subtype 4
    }
    function JUUID(uuid) {
        var hex = uuid.replace(/[{}-]/g, ""); // remove extra characters
        var msb = hex.substr(0, 16);
        var lsb = hex.substr(16, 16);
        msb = msb.substr(14, 2) + msb.substr(12, 2) + msb.substr(10, 2) + msb.substr(8, 2) + msb.substr(6, 2) + msb.substr(4, 2) + msb.substr(2, 2) + msb.substr(0, 2);
        lsb = lsb.substr(14, 2) + lsb.substr(12, 2) + lsb.substr(10, 2) + lsb.substr(8, 2) + lsb.substr(6, 2) + lsb.substr(4, 2) + lsb.substr(2, 2) + lsb.substr(0, 2);
        hex = msb + lsb;
        var base64 = HexToBase64(hex);
        return base64;
    }
    function CSUUID(uuid) {
        var hex = uuid.replace(/[{}-]/g, ""); // remove extra characters
        var a = hex.substr(6, 2) + hex.substr(4, 2) + hex.substr(2, 2) + hex.substr(0, 2);
        var b = hex.substr(10, 2) + hex.substr(8, 2);
        var c = hex.substr(14, 2) + hex.substr(12, 2);
        var d = hex.substr(16, 16);
        hex = a + b + c + d;
        var base64 = HexToBase64(hex);
        return base64;
    }
    function PYUUID(uuid) {
        var hex = uuid.replace(/[{}-]/g, ""); // remove extra characters
        var base64 = HexToBase64(hex);
        return base64;
    }
    function toBase64(base64) {
        return '<span class="Str">' + base64 + '</span>';
    }
    function toUUID(base64) {
        var hex = Base64ToHex(base64); // don't use BinData's hex function because it has bugs in older versions of the shell
        var uuid = hex.substr(0, 8) + '-' + hex.substr(8, 4) + '-' + hex.substr(12, 4) + '-' + hex.substr(16, 4) + '-' + hex.substr(20, 12);
        return '<span class="Type">UUID</span>(<span class="Str">\'' + uuid + '\'</span>)';
    }
    function toJUUID(base64) {
        var hex = Base64ToHex(base64); // don't use BinData's hex function because it has bugs in older versions of the shell
        var msb = hex.substr(0, 16);
        var lsb = hex.substr(16, 16);
        msb = msb.substr(14, 2) + msb.substr(12, 2) + msb.substr(10, 2) + msb.substr(8, 2) + msb.substr(6, 2) + msb.substr(4, 2) + msb.substr(2, 2) + msb.substr(0, 2);
        lsb = lsb.substr(14, 2) + lsb.substr(12, 2) + lsb.substr(10, 2) + lsb.substr(8, 2) + lsb.substr(6, 2) + lsb.substr(4, 2) + lsb.substr(2, 2) + lsb.substr(0, 2);
        hex = msb + lsb;
        var uuid = hex.substr(0, 8) + '-' + hex.substr(8, 4) + '-' + hex.substr(12, 4) + '-' + hex.substr(16, 4) + '-' + hex.substr(20, 12);
        return '<span class="Type">JUUID</span>(<span class="Str">\'' + uuid + '\'</span>)';
    }
    function toCSUUID(base64) {
        var hex = Base64ToHex(base64); // don't use BinData's hex function because it has bugs in older versions of the shell
        var a = hex.substr(6, 2) + hex.substr(4, 2) + hex.substr(2, 2) + hex.substr(0, 2);
        var b = hex.substr(10, 2) + hex.substr(8, 2);
        var c = hex.substr(14, 2) + hex.substr(12, 2);
        var d = hex.substr(16, 16);
        hex = a + b + c + d;
        var uuid = hex.substr(0, 8) + '-' + hex.substr(8, 4) + '-' + hex.substr(12, 4) + '-' + hex.substr(16, 4) + '-' + hex.substr(20, 12);
        return '<span class="Type">CSUUID</span>(<span class="Str">"' + uuid + '"</span>)';;
    }
    function toPYUUID(base64) {
        var hex = Base64ToHex(base64); // don't use BinData's hex function because it has bugs
        var uuid = hex.substr(0, 8) + '-' + hex.substr(8, 4) + '-' + hex.substr(12, 4) + '-' + hex.substr(16, 4) + '-' + hex.substr(20, 12);
        return '<span class="Type">PYUUID</span>(<span class="Str">\'' + uuid + '\'</span>)';
    }
    function toHexUUID(subtype, base64) {
        var hex = Base64ToHex(base64); // don't use BinData's hex function because it has bugs
        var uuid = hex.substr(0, 8) + '-' + hex.substr(8, 4) + '-' + hex.substr(12, 4) + '-' + hex.substr(16, 4) + '-' + hex.substr(20, 12);
        return '<span class="Type">HexData</span>(' + subtype + ', <span class="Str">\'' + uuid + '\'</span>)';
    }
</script>