亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

javascript將16進(jìn)制的字符串轉(zhuǎn)換為10進(jìn)制整數(shù)hex

 更新時(shí)間:2020年03月05日 00:22:56   作者:fareast_mzh  
這篇文章主要介紹了javascript將16進(jìn)制的字符串轉(zhuǎn)換為10進(jìn)制整數(shù)hex,需要的朋友可以參考下

16進(jìn)制的字符串 轉(zhuǎn)換為整數(shù)

function hex2int(hex) {
    var len = hex.length, a = new Array(len), code;
    for (var i = 0; i < len; i++) {
        code = hex.charCodeAt(i);
        if (48<=code && code < 58) {
            code -= 48;
        } else {
            code = (code & 0xdf) - 65 + 10;
        }
        a[i] = code;
    }
    
    return a.reduce(function(acc, c) {
        acc = 16 * acc + c;
        return acc;
    }, 0);
}

// 15 + 16 * 13 + 256 = 479
console.log(hex2int("1df"));

十進(jìn)制整數(shù)轉(zhuǎn)換16進(jìn)制

function int2hex(num, width) {
  var hex = "0123456789abcdef";
  var s = "";
  while (num) {
	s = hex.charAt(num % 16) + s;
	num = Math.floor(num / 16);
  }
  if (typeof width === "undefined" || width <= s.length) {
	return "0x" + s;
  }
  var delta = width - s.length;
  var padding = "";
  while(delta-- > 0) {
	padding += "0";
  }
  return "0x" + padding + s;
}

console.log(int2hex(479, 8));

0x000001df

下面是補(bǔ)充資料

JS-Ascii碼中字符與十進(jìn)制/十六進(jìn)制相互轉(zhuǎn)換

如上述圖ASCII標(biāo)準(zhǔn)表中,想將字符“1”轉(zhuǎn)換成十進(jìn)制或十六進(jìn)制,實(shí)現(xiàn)方法:

var charData = '1';
charData.charCodeAt();    //輸出結(jié)果為上表中‘1'對(duì)應(yīng)的十進(jìn)制數(shù)據(jù):49
charData.charCodeAt().toString(16);  //輸出結(jié)果為上表中‘1'對(duì)應(yīng)的十六進(jìn)制數(shù)據(jù):31

若想將不同進(jìn)制數(shù)據(jù)轉(zhuǎn)換成對(duì)應(yīng)字母的實(shí)現(xiàn)方法:

var num = 49;
String.fromCharCode(num);  //輸出49對(duì)應(yīng)的字符 '1'

JS中字符問(wèn)題(二進(jìn)制/十進(jìn)制/十六進(jìn)制及ASCII碼之間的轉(zhuǎn)換)

var a='11160'; 
alert(parseInt(a,2)); //將111做為2進(jìn)制來(lái)轉(zhuǎn)換,忽略60(不符合二進(jìn)制),從左至右只將符合二進(jìn)制數(shù)的進(jìn)行轉(zhuǎn)換 
alert(parseInt(a,16)); //將所有的都進(jìn)行轉(zhuǎn)換 
依照此方法,其實(shí)可以轉(zhuǎn)換成任何進(jìn)制 
var a='1110'; 
alert(parseInt(a,10).toString(16)); //將A轉(zhuǎn)換為10進(jìn)制,然后再轉(zhuǎn)換成16進(jìn)制 同樣也可以是其它進(jìn)制 
下面說(shuō)下ASCII 碼: 
function test(){ 
var a='ab'; 
var c=a.charCodeAt(1);//返回98 也就是b的AscII碼 位置從0開(kāi)始 
 
var char=String.fromCharCode(98);返回小寫(xiě)的b 
} 
//小例子 
function test(){ //輸出AscII碼擴(kuò)展集中的字符 
var c=""; 
for(var i=1;i<65536;i++){ 
if((i%10)==0){ 
c+=i+':\t'+String.fromCharCode(i)+'\t'+'\n';}else{ 
c+=i+':\t'+String.fromCharCode(i)+'\t';} 
} 
document.getElementById("abc").innerText=c; 
} 
<div id='abc'></div> 

js字符與ASCII碼互轉(zhuǎn)的方法

大寫(xiě)字母A-Z對(duì)應(yīng)的ASCII碼值是65-90
小寫(xiě)字母a-z對(duì)應(yīng)的ASCII碼值是97-122

將字母轉(zhuǎn)為ascii嘛的方法:

var str = "A";
str.charCodeAt(); // 65

var str1 = 'a';
str1.charCodeAt(); // 97

將ascii碼轉(zhuǎn)為對(duì)應(yīng)字母的方法:

var num = 97;
String.fromCharCode(num); // 'a'

var num1 = 100;
String.fromCharCode(num1); // 'd'

以上就是javascript將16進(jìn)制的字符串轉(zhuǎn)換為10進(jìn)制整數(shù)hex的詳細(xì)內(nèi)容,更多關(guān)于16進(jìn)制的字符串轉(zhuǎn)換為10的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論