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

I'm trying to create the equivalent of PHP's unpack. I've noticed the project PHPJS does

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;
}
)()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744415958a4573119.html

相关推荐

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

    I'm trying to create the equivalent of PHP's unpack. I've noticed the project PHPJS does

    6天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信