背景
我在初學nodejs這門語言的時候,遇到了16進制無法正常轉成2進制的問題。
解決思路
先是對一個字符串使用toString(2)。然而發現字符串再使用這種方法,已經沒有意義了。
16進制轉2進制的過程:16進制字符串->數字->2進制數
<code>weekdatebin
=weekdate.slice(0
,8
)weekdatebin
=parseInt(weekdatebin,"16"
).toString(2
)weekdatebin
=PrefixInteger(weekdatebin,32
) /<code>
但也有問題,24個十六進制數,是無法轉成24*4的2進制數的,太長了,會報錯。
一種解決方案,就是將這24位的數拆分成3個8位進行處理,還有一種方式則是通過C語言的按位與思想,但是沒有深入嘗試了。
<code>//轉成二進制,按位讀取 24位分3次處理 console.log(weekdate); weekdatebin=weekdate.slice(0,8); weekdatebin=parseInt(weekdatebin,"16"
).toString(2); weekdatebin=PrefixInteger(weekdatebin,32); console.log(weekdatebin); weekdatebin=weekdate.slice(8,16); weekdatebin=parseInt(weekdatebin,"16"
).toString(2); weekdatebin=PrefixInteger(weekdatebin,32); console.log(weekdatebin); weekdatebin=weekdate.slice(16,24); weekdatebin=parseInt(weekdatebin,"16"
).toString(2); weekdatebin=PrefixInteger(weekdatebin,32); console.log(weekdatebin); /<code>
第二天學習nodejs,如果覺得太簡單了,日後還會更新高深一點的博客。