zoukankan      html  css  js  c++  java
  • js 16进制转10进制

    函数定义:

    // 16进制数转10进制
    var ex16hex = function(value){
      value = stripscript(value);
        value = value.replace("0x","");
      var arr = value.split("");
      arr = arr.reverse();
      var len = arr.length;
      var res = 0;
      $.each(arr, function(i,v){
          var num = hex_change(v);
        console.log(num)
          res += muti16(num, i);
      });
      return res;
    }
    
    // 字符转16进制数字
    var hex_change = function(v){
        var res;
        switch(v){
          case "a": res = 10;break;
        case "b": res = 11;break;
        case "c": res = 12;break;
        case "d": res = 13;break;
        case "e": res = 14;break;
        case "f": res = 15;break;
        case "1":
        case "2":
        case "3":
        case "4":
        case "5":
        case "6":
        case "7":
        case "8":
        case "9": res = Number(v);break;
        default: res = 0;break;
      }
      return res;
    }
    
    // 过滤所有特殊字符
    var stripscript = function(s) {
        var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?↵\r\n]");
            var rs = "";
        for (var i = 0; i < s.length; i++) {
            rs = rs + s.substr(i, 1).replace(pattern, '');
        }
        return rs;
    }
    
    // 返回 v 乘以 n 个 16 的积
    var muti16 = function(v, n){
      var temp = v;
        for(var i = 0; i < n; i++){
        temp *= 16;
      }
      return temp;
    }

    使用例子:

    var a = "0x1c", b = "1c";
    console.log("a = "+ ex16hex(a)); // a = 28
    console.log("b = "+ ex16hex(b)); // b = 28
  • 相关阅读:
    linux安装pip
    keepalived 安装和配置
    SecureCRT突然卡死的问题
    python url合并与分离
    centos 长久路由表
    linux shell中 if else以及大于、小于、等于逻辑表达式介绍
    mtime,ctime,atime
    linux学习初体验
    Python数据库连接池DBUtils
    Flask 中的蓝图(BluePrint)
  • 原文地址:https://www.cnblogs.com/mankii/p/9886119.html
Copyright © 2011-2022 走看看