te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Converting octal and hexadecimal numbers to base 10 - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Converting octal and hexadecimal numbers to base 10 - Stack Overflow

programmeradmin3浏览0评论

I am trying to understand javascript octal and hexadecimal putations. I know I can use parseInt(string, radix) to get the Integer value.

For example, when I try this why are the values different?

var octal = parseInt('026', 8); 
var octal1 = parseInt('030', 8); 
alert(octal); //22
alert(octal1); //24    

var hex = parseInt('0xf5', 16); 
var hex1 = parseInt('0xf8', 16); 
alert(hex); //245
alert(hex1); //248

But if I try to save it in an array why are the answers different and incorrect?

var x = new Array(026, 5, 0xf5, "abc");
var y = new Array(030, 3, 0xf8, "def");

alert('026 is ' + parseInt(x[0],8)); //18
alert('0xf5 is ' + parseInt(x[2],16)); //581
alert('030 is ' + parseInt(y[0],8)); //20
alert('0xf8 is ' + parseInt(y[2],16)); //584

I am trying to understand javascript octal and hexadecimal putations. I know I can use parseInt(string, radix) to get the Integer value.

For example, when I try this why are the values different?

var octal = parseInt('026', 8); 
var octal1 = parseInt('030', 8); 
alert(octal); //22
alert(octal1); //24    

var hex = parseInt('0xf5', 16); 
var hex1 = parseInt('0xf8', 16); 
alert(hex); //245
alert(hex1); //248

But if I try to save it in an array why are the answers different and incorrect?

var x = new Array(026, 5, 0xf5, "abc");
var y = new Array(030, 3, 0xf8, "def");

alert('026 is ' + parseInt(x[0],8)); //18
alert('0xf5 is ' + parseInt(x[2],16)); //581
alert('030 is ' + parseInt(y[0],8)); //20
alert('0xf8 is ' + parseInt(y[2],16)); //584
Share Improve this question edited Feb 23, 2012 at 19:58 Phrogz 303k113 gold badges667 silver badges756 bronze badges asked Feb 23, 2012 at 19:35 theking963theking963 2,2267 gold badges29 silver badges42 bronze badges 2
  • You are right. The first part is unnecessary. alert(026) will yield 22. How about the arrays? – theking963 Commented Feb 23, 2012 at 19:47
  • 1 Note that var foo = new Array(...); is almost never preferable over var foo = [...]; as the former is more bytes, slower, and functionally equivalent. – Phrogz Commented Feb 23, 2012 at 19:49
Add a ment  | 

3 Answers 3

Reset to default 7

parseInt converts the argument to string before parsing it:

  1. Let inputString be ToString(string)

So:

0x35.toString() // "53"
parseInt( "53", 16 ); //83
parseInt( 0x35, 16 ); //83

What you have in your arrays are mostly already numbers so there is no need to parse them. You will get expected results if you change them to strings:

var x = new Array("026", "5", "0xf5", "abc");
var y = new Array("030", "3", "0xf8", "def");

I've written a function that can convert a number from one base to another, where the starting base and the new base are specified as parameters.

function convertFromBaseToBase(str, fromBase, toBase){
    var num = parseInt(str, fromBase);
    return num.toString(toBase);
}

alert(convertFromBaseToBase(10, 2, 10));

http://jsfiddle/jarble/p9ZLa/

0xf5 seen outside a string is the integer 245; it is not a string that needs to be parsed:

console.log( "0xf5" );              //-> 0xf5
console.log( parseInt("0xf5",16) ); //-> 245
console.log( 0xf5 );                //-> 245
console.log( parseInt(0xf5,16)  );  //-> 581
console.log( parseInt("245",16) );  //-> 581
console.log( parseInt(245,16)   );  //-> 581

Similarly, 026 outside a string is the integer 22:

console.log( 026 );                 //-> 22

What you're doing is passing already-converted-to-base10 integers to parseInt(), which then converts them to strings and interprets the base10 as base16, performing the base conversion a second time.

发布评论

评论列表(0)

  1. 暂无评论