码迷,mamicode.com
首页 > 其他好文 > 详细

在线任意进制转换工具 - aTool在线工具

时间:2016-07-15 19:49:31      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

 

http://www.atool.org/hexconvert.php

 

ss = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@";
function v10toX(n, m) {
    m = String(m).replace(/ /gi, "");
    if (m == "") {
        return ""
    }
    var a = ss.substr(0, 10);
    var b = a + ".";
    if (eval("m.replace(/[" + b + "]/gi,‘‘)") != "") {
        M("请输入有效的进制数!");
        return ""
    }
    m = m.split(".");
    if (m.length > 2) {
        M("请输入有效的进制数!");
        return ""
    }
    var a = ss.substr(0, n);
    if (m.length == 1) {
        m = m[0];
        var t = "";
        while (m != 0) {
            var b = m % n;
            t = a.charAt(b) + t;
            m = (m - b) / n
        }
        return t
    } else {
        var m0 = m[0];
        var t = "";
        while (m0 != 0) {
            var b = m0 % n;
            t = a.charAt(b) + t;
            m0 = (m0 - b) / n
        }
        var cnt = 18;
        var m1 = m[1];
        m1 = parseFloat("0." + m1);
        var d = "", b = 0;
        while (m1 != 0 && cnt > 0) {
            m1 = m1 * n;
            b = parseInt(m1);
            d = d + a.charAt(b);
            m1 = m1 - b;
            cnt--
        }
        return t + "." + d
    }
}
function vXto10(n, m) {
    m = String(m).replace(/ /gi, "");
    if (m == "") {
        return ""
    }
    var a = ss.substr(0, n);
    var b = a + ".";
    if (eval("m.replace(/[" + b + "]/gi,‘‘)") != "") {
        M("请输入有效的" + n + "进制数!");
        return ""
    }
    m = m.split(".");
    if (m.length > 2) {
        M("请输入有效的" + n + "进制数!");
        return ""
    }
    if (m.length == 1) {
        m = m[0];
        var t = 0, c = 1;
        for (var x = m.length - 1; x > -1; x--) {
            t += c * (a.indexOf(m.charAt(x)));
            c *= n
        }
        return t
    } else {
        var m0 = m[0];
        var t = 0, c = 1;
        for (var x = m0.length - 1; x > -1; x--) {
            t += c * (a.indexOf(m0.charAt(x)));
            c *= n
        }
        var m1 = m[1];
        var d = 0, c = 1 / n;
        for (var x = 0; x < m1.length; x++) {
            d += c * (a.indexOf(m1.charAt(x)));
            c /= n
        }
        return t + d
    }
}
function vXtoY(d, b, c) {
    a = vXto10(d * 1, b);
    if (a == "") {
        return ""
    }
    a = v10toX(c, a);
    return a
}
function M(b) {
    alert(b)
}
function convert(g, h, i, f) {
    var e = document.getElementById(h).value;
    var d = document.getElementById(f);
    var b = document.getElementById(g).value;
    var c = document.getElementById(i).value;
    d.value = vXtoY(b, e, c)
}
function convert_c(g, e, f, d) {
    var c = document.getElementById(e).value;
    var b = document.getElementById(d);
    b.value = vXtoY(g, c, f)
};

 

<div class="row">
<span class="span strong">二 进 制:</span>
<div class="span four xs-four">
<input type="text" id="bin_1" alt="二进制数" title="二进制数" class="input-text" value="10011110101110110100010">
</div>
<span class="span strong">十 进 制:</span>
<div class="span four xs-four">
<input type="text" id="ten_1" alt="十进制数" title="十进制数" class="input-text" value="">
</div>
<button class="button primary small" type="button" onclick="convert_c(2, ‘bin_1‘, 10, ‘ten_1‘);">转换</button>
</div>

 

在线任意进制转换工具 - aTool在线工具

标签:

原文地址:http://www.cnblogs.com/exmyth/p/5674560.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!