最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

unpack - How to write a base32_decode in JavaScript? - Stack Overflow

programmeradmin7浏览0评论

I'm trying to create the equivalent of PHP's unpack. I've noticed the project PHPJS doesn't have it. I need it for the implementation of base32_encode and base32_decode (using Crockford's alphabet '0123456789ABCDEFGHJKMNPQRSTVWXYZ').

I couldn't find it anywhere and judging from it's counterpart, PHPJS's pack function I doubt my version will be plete and bug free any time soon.

I'm trying to create the equivalent of PHP's unpack. I've noticed the project PHPJS doesn't have it. I need it for the implementation of base32_encode and base32_decode (using Crockford's alphabet '0123456789ABCDEFGHJKMNPQRSTVWXYZ').

I couldn't find it anywhere and judging from it's counterpart, PHPJS's pack function I doubt my version will be plete and bug free any time soon.

Share Improve this question edited Jun 15, 2011 at 20:28 Brett Zamir 14.4k7 gold badges57 silver badges83 bronze badges asked May 27, 2011 at 15:23 Valentin BrassoValentin Brasso 1,3777 gold badges21 silver badges42 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3
base32tohex = (function() {
    var dec2hex = function(s) {
        return (s < 15.5 ? "0" : "") + Math.round(s).toString(16)
    }
      , hex2dec = function(s) {
        return parseInt(s, 16)
    }
      , base32tohex = function(base32) {
        for (var base32chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", bits = "", hex = "", i = 0; i < base32.length; i++) {
            var val = base32chars.indexOf(base32.charAt(i).toUpperCase());
            bits += leftpad(val.toString(2), 5, "0")
        }
        for (i = 0; i + 4 <= bits.length; i += 4) {
            var chunk = bits.substr(i, 4);
            hex += parseInt(chunk, 2).toString(16)
        }
        return hex
    }
      , leftpad = function(str, len, pad) {
        return len + 1 >= str.length && (str = new Array(len + 1 - str.length).join(pad) + str),
        str
    };
    return base32tohex;
}
)()
发布评论

评论列表(0)

  1. 暂无评论