• 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
  • 相关阅读:
    NGINX基本概念
    IP地址进制转换
    路由
    ip ,网段, 网关
    ipaddress模块
    第53课 被遗弃的多重继承(上)
    const static valitate 区别
    第49课 多态的概念和意义 (虚函数virtual)
    第75课 图的遍历(深度优先遍历DFS)
    第74课 图的遍历(广度优先遍历BFS)
  • 原文地址:https://www.cnblogs.com/mankii/p/9886119.html
Copyright © 2020-2023  润新知