發表文章

Arduino控制WS2812三色燈條

圖片
本文用到的材料為: WS2812B LED 燈條 x 1 Arduino UNO x 1 杜邦線公對公 x 3 軟體為 Arduino IDE 1.8.5 搭配 FastLED程式庫 。 安裝 FastLED 程式庫 打開Arduino 的程式庫管理工具(草稿碼 ==> 匯入程式庫 ==> 管理程式庫) 搜尋FastLED 安裝好FastLED就可以看到內建的範例程式 Arduino電路 LED燈條 5V 接 Arduion 5V LED燈條 GND 接 Arduion GND LED燈條 ID 接 Arduion Pin 5 接好電路後,筆者使用【檔案 ==> 範例 ==> FastLED ==> ColorPalette】來試試看,ColorPalette的Demo影片如下: 因為程式裡設定使用 50 顆 LED,所以此燈條沒有全部亮。 參考資料: [1] Guide for WS2812B Addressable RGB LED Strip with Arduino 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 If you like this post, please click the ads on the blog or  buy me a coffee . Thank you very much.

Python Expressive Puzzlers 8: Does Equal

底下的Java程式會輸出什麼結果? 1 2 3 4 5 6 7 8 public class DosEquis { public static void main ( String [] args ) { char x = 'X' ; int i = 0 ; System . out . print ( true ? x : 0 ); System . out . print ( false ? i : x ); } } 輸出結果會是XX嗎?根據 JLS 15.25 的說明 ( true ? x : 0 ) 的運算結果會是char型別,而 ( false ? i : x ) 的運算結果會是int型別。所以輸出結果為 X88 。 而 Python 只有  < expression1 > if < condition > else < expression2 > 的語法,沒有 Java 的 conditional operator 的語法,於是不會有這樣子的Pitfall。 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

Python Expressive Puzzlers 7: Swap

底下兩個Java Class分別會輸出什麼結果? 1 2 3 4 5 6 7 8 public class CleverSwapOne { public static void main ( String [] args ) { int x = 1984 ; int y = 2001 ; x ^= y ^= x ^= y ; System . out . println ( "x = " + x + "; y = " + y ); } } 1 2 3 4 5 6 7 8 9 10 public class CleverSwapTwo { public static void main ( String [] args ) { int x = 1984 ; int y = 2001 ; x ^= y ; y ^= x ; x ^= y ; System . out . println ( "x = " + x + "; y = " + y ); } } CleverSwapOne 會輸出 x = 0; y = 1984 ,而 CleverSwapTwo 會輸出 x = 2001; y = 1984 。為什麼會這樣子呢? 根據 JLS 15.7 的說明 :「Evaluate Left-Hand Operand First」也就是 CleverSwapOne  的執行順序如下: 1 2 3 4 5 6 7 // The actual behavior of x ^= y ^= x ^= y in Java int tmp1 = x ; // First appearance of x in the expression int tmp2 = y ; // First appearance of y int tmp3 = x ^ y ; // Compute x ^ y x = tmp3 ; // L...

「特洛伊木馬病毒程式設計--使用Python」讀後感

特洛伊木馬病毒程式設計--使用Python   此書在前言部分就說明, 請以學術研究的角度來看待此書 。這讓筆者想起多年前曾去參加一個資安的活動,內容是說明什麼是buffer overflow、stack overflow、SQL injection 等主題,而近幾年隨著 python 程式語言的流行,也越來越多用 python 來寫一些五花八門功能的程式。 而導讀部分也提到只要權限夠(通常用Kernel mode),用C/C++指標的功能可以存取到特定記憶體位址,進而做一些特異功能。此書透過檔案讀寫、網路功能、執行緒、鍵盤紀錄等四大程式功能來完成一個可收集 Windows User 按了哪幾個鍵盤。 而熟悉如何使用這四大功能的人,大概也不想看這本書了吧!底下列出一些參考資源給有興趣的人去研究。 1. Python File IO 2. Python Network Programming 3. Python Multithreading 4. Python Keylogger 依照這四大功能,此書從網路程式觀念開始慢慢地建立讀者的基本Socket程式觀念,接著說明如何用網路程式來傳送檔案以及通訊協定的介紹。之後,多工程式常用的執行緒上場了。然後就是最後一項功能:鍵盤紀錄(這方法限制在Windows系統上)。 OK,那麼本書的特洛伊木馬病毒程式範例到底是什麼呢?因為本書的範例是在虛擬機器上測試,而且相關的防火牆也需要關閉,也得開啟權限讓程式執行。而 這離真正的木馬程式還有一段路要走 。不過作者也提到,只要將程式修改,或是將觀念融會貫通並實作出來,就有機會越來越接近真實的木馬程式了。 BTW,書中提到一個數字:「1337」,想了解此數字代表什麼意思,請參考 這裡 ]-[3I23 或 那裡 7]-[3I23 。 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 If you like this post, please click the ads on the blog or  buy me a coffee . Thank you very much.

Python Expressive Puzzlers 6: Multicast

底下的Java程式會輸出什麼? 1 2 3 4 5 public class Multicast { public static void main ( String [] args ) { System . out . println (( int ) ( char ) ( byte ) - 1 ); } } 結果不會是 -1。幸運的是有條簡單的規則來說明:「Sign extension is performed if the type of the original value is signed; zero extension if it is a char, regardless of the type to which it is being converted.」也就是 byte -1  === sign extension ==> char 65535 === zero extension ==> int 65535。 而 Python Built-in type 不多,筆者只試了底下兩行: print ( int ( str ( bytes (- 1 )))) print ( int ( chr ( bytes (- 1 )))) 輸出結果會是什麼呢? 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 If you like this post, please click the ads on the blog or  buy me a coffee . Thank you very much.

Python Expressive Puzzlers 5: The Joy of Hex

下面的程式碼會輸出什麼結果? 1 2 3 4 5 public class JoyOfHex { public static void main ( String [] args ) { System . out . println ( Long . toHexString ( 0x100000000 L + 0xcafebabe )); } } 答案當然不會是1cafebabe。這是因為  0xcafebabe  會被擴展成  0x100000000L  的 long 型別,也就是從 int 變成 long。那這會讓  0xcafebabe  變成  0xffffffff cafebabeL  ,原因是因為  0xcafebabe  的最高位元為1,當十六進制與八進制的最高位元為1時,Java會當成是負數來處理,也就是一直補1到左邊的高位元裡。而  0x100000000L + 0xcafebabe  就會變成 0x00000001 00000000L + 0xffffffff cafebabeL 運算結果為  0xcafebabe  。只要將  0xcafebabe  補上L變成  0xcafebabe L  就可以避免這個問題。  那Python呢? print ( hex ( 0x100000000L + 0x cafebabe )) 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 If you like this post, please click the ads on the blog or  buy me a coffee . Thank you very much.

Python Expressive Puzzlers 4: It’s Elementary

底下Java程式的執行結果是什麼? 1 2 3 4 5 public class Elementary { public static void main ( String [] args ) { System . out . println ( 12345 + 5432 l ); } } 答案是 17777 。數字1和小寫的L(l)是不一樣的。所以"Java Puzzlers"作者建議如下: 「 Always use a capital el (L) in long literals, never a lowercase el (l).」 在long literals時,請用大寫的L。 那Python呢? print ( 12345 + 5432 l ) 上述片段的Python程式碼是從VS code複製過來的,此IDE幫我們將小寫L標上不同的顏色了! 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 If you like this post, please click the ads on the blog or  buy me a coffee . Thank you very much.