2025年12月29日 星期一

再探 Uint8Array.fromHex()

 再探 Uint8Array.fromHex()

前言

  在先前的 初探 Uint8Array.fromHex() 與 Uint8Array.toHex() 使用 Uint8Array.fromHex() 來編碼成 Hex ,Uint8Array.fromHex() 是如何編碼儲存的呢?在此把學習的過程做個紀錄。


內容

  在開始前先說明一下 初探 Uint8Array.fromHex() 與 Uint8Array.toHex() 會容易讓人以為可以將任何字串做編碼,但事實上只有符合 16 進制的字元( 0 ~ 9 與 a~f ) 可以被編碼,如果該字串含有非 16 進制的字元( 0 ~ 9 與 a~f ) 將會報錯。範例如下

let str = '0123456789abcdef';
let arHex = Uint8Array.fromHex( str);
console.log( arHex.toString() );//1,35,69,103,137,171,205,239
arHex.forEach( (val) => console.log( "0x"+val.toString(16).padStart(2,'0') ) );
// "0x01"
// "0x23"
// "0x45"
// "0x67"
// "0x89"
// "0xab"
// "0xcd"
// "0xef"


透過 Uint8Array.toString() 可以看到儲存的時候只有 8 個,但輸入的字串有 16 個,所以猜測是以每兩個 Byte 編碼成一個 Uint8 ,結果呢?沒錯,透過範例最後的 forEach() 可以看到每個 Uint8 編碼是來自兩個 Byte ,第一個 ox01 是來自原始字串的 o1 ,注意高位元是 0 ,低位元是 1 ,所以編碼成 0x01 ,而非0x10 。

 

參考資料

[ developer.mozilla.org ] Uint8Array.fromHex()


相關文章與資料

初探 Uint8Array.fromHex() 與 Uint8Array.toHex()

2025年12月22日 星期一

初探 Uint8Array.fromHex() 與 Uint8Array.toHex()

 初探 Uint8Array.fromHex() 與 Uint8Array.toHex()

前言

  JavaScript 提供透過 Uint8Array 轉換十六進制,那就是 Uint8Array.fromHex() 與 Uint8Array.toHex() ,在此把學習的過程做個紀錄。


內容

  範例如下

let str = '0123456789abcdef';
let arHex = Uint8Array.fromHex( str);
console.log( arHex.constructor.name );//Uint8Array
console.log( arHex.toString() );//1,35,69,103,137,171,205,239
console.log( arHex.toHex() );//0123456789abcdef


Uint8Array.fromHex() 與 Uint8Array.toHex() 的用法很簡單,透過 Uint8Array.fromHex() 來編碼,注意編碼完後的型別是 Uint8Array ,接著透過 toString() 印出編碼後的結果,要解碼透過 Uint8Array.toHex() 直接解碼即可。


參考資料

[ developer.mozilla.org ] Uint8Array.fromHex()

[ developer.mozilla.org ] Uint8Array.prototype.toHex()

2025年12月15日 星期一

注意 NaN 的型別

 注意 NaN 的型別

前言

  在先前的 注意 NaN 的比較 提到 NaN 的比較,這次來看 NaN 的型別,NaN 的型別有些特別,在此把學習的過程做個紀錄。


內容

  範例如下

console.log( typeof NaN );//Number
console.log( 1 + NaN );//NaN
console.log( NaN + 1 );//NaN
//
console.log( parseInt('A') );//NaN
console.log( parseFloat('A') );//NaN


NaN 的型別是 Number ,這有有點反直覺,因為 NaN 的全名是 Not A Number ,這有點特別!因為是型別是 Number ,所以可以搭配運算子運算,運算的規則很簡單,任何跟 NaN 運算的結果都會變成 NaN ,不論是後算 NaN 或 先算 NaN 都是一樣的結果。最後,不只是運算的過程會產生 NaN ,有些 Function 的結果也會回傳 NaN ,如 parseInt() 與 parseFloat() 。


參考資料

[ medium.com/andy-blog ] JavaScript 有趣的冷知識 :神奇的 NaN


相關文章與資料

初探 NaN

注意 NaN 的比較

2025年12月8日 星期一

注意 NaN 的比較

 注意 NaN 的比較

前言

  在先前的 初探 NaN 處不認識了 NaN , NaN 在比較時的特性比較特別,在此把學習的過程做個紀錄。


內容

  範例如下

console.log( NaN === NaN);//false
console.log( NaN == NaN);//false
console.log( isNaN( NaN ) );//true


NaN 在 === 與 == 的比較都匯回傳 false! NaN 自己不等於自己,如果要比較還是透過 isNaN() 來比較才會如預期。


參考資料

[ medium.com/andy-blog ] JavaScript 有趣的冷知識 :神奇的 NaN


相關文章與資料

初探 NaN

2025年12月1日 星期一

初探 NaN

 初探 NaN

前言

  NaN 是 JavaScript 特有的常數,以前都沒好好了解,只知道是個常數,這次就來認識 NaN ,在此把學習的過程做個紀錄。


內容

  NaN 是個常數,全名是 Not A Number ,直翻就是"不是個數字",可以用 isNaN() 來檢測,範例如下

console.log( isNaN( 123 ) );//false
console.log( isNaN( "123" ) );//false
console.log( isNaN( "abc" ) );//true
console.log( isNaN( Infinity ) );//false
console.log( isNaN( -Infinity ) );//false
console.log( isNaN( NaN ) );//true
console.log( isNaN( {} ) );//true
console.log( isNaN( [] ) );//false


範例先使用數字(123)來測試,結果如期望是 false ,接著測試字串,這裡分別測試字串內容是數字與字串內容不是數字的狀況,結果當內容是數字結果為 false ,反之則是 true ,字串的檢測會依據內容作改變。那 Infinity 呢?不論是 Infinity 與 -Infinity 的結果都是 false ,Infinity 在 JavaScript 歸類為數字。接著直接拿 NaN 檢測如預期為 true。最後做了有趣的測試,如果拿 Object 與 Array 來檢測結果如何呢? Object 如預期是 true ,但意外的是 Array ,結果是 false,雖然正常不會這樣使用,但這結果倒是滿讓人意外的。


參考資料

[ developer.mozilla.org ] NaN