在C#上使用類似sprintf的字串轉換

Solving Constraints

English Summary: This article describe about how to use String.Format to implement the same with sprintf or printf in C++. For English, Please refer this article for more detail.

中文: 最近遇到一些好玩的現象~~ 再次就把解決方式寫下來吧~~ (這篇就不用寫英文了,要看英文的部份請看這篇文章)。

最近在C#上面,想要輸出固定2個位元小數點浮點數~ 為了這件事情~~ 覺得有點難搞~~ 由於傳下來的數字是Double 來儲存的,並且有可能是循環小數 (比如說: 10/3 = 3.333)。 如果~~ 你單純的使用Math.Round() 比如以下的範例:

Double buf = 3.33333333333333333; buf = Math.Round(buf , 2); > >

這個時候~ 你還是會發現 buf 還是那一連串的小數點~ 而沒有再小數點第二位去取四捨五入。原因在於小數點數字過多~ 而產生了 exception 的發生。 這個時候我又想了一個方法,參考以下的code:

Double buf = 3.33333333333333333; buf = Math.Abs(buf*100); buf = bug/100; > >

很奇怪的~~ 這樣也是無法轉換到兩個小數點~ 還會變成原來的數字~~ (難道是最佳化?) 難道我得使用CallingConvention Enumeration  的方式,把sprintf 當作外在function call嗎?

Imports System Imports Microsoft.VisualBasic Imports System.Runtime.InteropServices Public Class LibWrap ' Visual Basic does not support varargs, so all arguments must be ' explicitly defined. CallingConvention.Cdecl must be used since the stack ' is cleaned up by the caller. ' int printf( const char *format [, argument]... ) <DllImport("msvcrt.dll", CallingConvention := CallingConvention.Cdecl)> _ Overloads Shared Function printf ( _     format As String, i As Integer, d As Double) As Integer End Function <DllImport("msvcrt.dll", CallingConvention := CallingConvention.Cdecl)> _ Overloads Shared Function printf ( _     format As String, i As Integer, s As String) As Integer End Function End Class 'LibWrap Public Class App     Public Shared Sub Main()         LibWrap.printf(ControlChars.CrLf + "Print params: %i %f", 99, _                        99.99)         LibWrap.printf(ControlChars.CrLf + "Print params: %i %s", 99, _                        "abcd")     End Sub 'Main End Class 'App > >

還好之後在網路上有找到相關的程式String.Format() 去做類似~ sprintf 的轉換.

Double buf = 3.33333333333333333; string outputStr = String.Format("{0:f}", buf); > >

要更多範例~~ 可以去這邊查詢

星光大道所帶來的影響

超級星光大道

超級星光大道~ 大概是最近最受人矚目的節目了吧~ 本來我對於這個節目的感受也是普普通通而已~~ 由於毒舌派歌唱節目已經有”快樂星期天“在領軍了。當初實在讓人懷疑這個節目能做多久。

不過~ 王偉忠果然不是簡單的人物~ 在不強調毒蛇裁判、強力推薦他們所比賽出來的選手之下~~ 漸漸的~~~ 就算我沒看過這個節目~ 在其他比如說”康熙來了”、”棒棒X”、”黑黑會” 等等節目之中~~ 都會了解到這個節目所選出來的選手~~ 當然~ 這個時候你或許會在新聞上面看到”楊宗緯”~~ 甚至是有所謂魔王等級的挑戰者”蕭敬騰“的出現之後  節目就開始好看了 大家就開始期待著~~ 兩個實力相當好的歌手相互激盪所帶出的火花。

好了~~ 講了那麼多的官方語句~~ 我就來談談 星光大道這個節目對於某些人的好處好了。

  • 七年級生:
    誰說七年級生是草莓族? 不論是恐怖的”蕭敬騰”~ 還是衛冕者”楊宗緯” 他們都只是個20出頭的學生~ 現在真的很感謝”錢櫃” 與”好樂迪” 幫我們造就了一群優秀的歌手。

  • 曹格:
    我相信~ 就算你跟我一樣~~ 沒聽過”曹格”的背叛~ 你大概會聽過”蕭敬騰“的背叛甚至是”楊宗緯
    “版本的”背叛“。讓人好笑的是~~ 去搜尋”曹格 背叛” 他們兩個的版本 ~ 竟然也是再搜尋的前五名之內~~ 可見~~ 曹格真的要好好感謝他們~~~

  • 山頂洞人跟周口店人:
    山頂洞人周口店人~~ 大概現在歷史不太好的七、八年級的同學~ 可能都不知道~~ 但是~~ 你應該都有看過楊宗緯 周口店人一了百了-我愛黑澀會 星光選手來踢館 這一點影片。 我們歷史老師~~ 真的應該感謝 超級星光大道~~~

最後~~ 放一段他們比賽的影片吧~~~

[youtube http://www.youtube.com/watch?v=8wXl5bZEwr0]

[NBA Live 06/07] 好用的補丁集合

好久沒把這塊遊戲再度拿出來玩~~~ 會把NBA Live 07拿出來玩的主因~~ 就是由於我的電腦中毒了  所以的遊戲都不見了~ 重新去安裝軟體的時候~ 翻了翻手邊遊戲之中  忽然又看到這塊讓我又愛又恨的遊戲~

由於07電腦換人與自幹的問題(改過PATCH 與 ROSTER之後仍然嚴重)~~ 我之前就先在網路上找NBA Live 06的最新球員更新檔~~ 

但是苦於找不到的情況下~ 先找到了這個更新檔~~ 不過在安裝Jimmy GameFix 系列之前~ 還是要先安裝 Live 2007 中文版更新檔 。之後再去這裡下載Jimmy GameFix 9.7 (雖然圖片是9.4~ 但是最新版本已經到達了9.7)

In 9.7 improvements from the previous version: 1. AI improvement 2. Re-new Team strategy for Defense 3. Re-new Team strategy for Offense 4. Different of Alleyoops 5. Dunking for Star Highflyers[credit to A_nuke] 6. Dynamic plays 7. More distribution of ball 8. Every players have chances to be more offensive 9. Some others improvement 10. Special edited AIACT 11. ONLY 100% work with IRPT roster 4.4 and 9x% work with IRPT roster 4.x > >

安裝方法~~ 裡面有兩個安裝檔~~ 就把他裝起來~~

  1. 安裝part1.exe
  2. 安裝part2.EXE
  3. Copy For Dynasty Season or PlayoffSettings,dat 換掉原來安裝的 (這是調整Slider)
  4. Copy TEAMPLAYFIX FOR NOinjury ROSTER(compatible with all types of saves) 到你的王朝紀錄檔下~ 執行該BAT 可以幫你降低一些素質.

此外~~要 NBA Live 06 的最新球員更新檔??到這裡去找吧~~~

減肥人的福音~~ skinnyr 每天記錄你的體重~並且顯示線型圖

如果你跟我一樣~~是個為了體重而困擾的人

(不過~ 我那麼胖~~ 可能罪惡感比較少~~)

http://www.skinnyr.com/ 就是個相當好的一個網站~~

它提供了一個簡單但是對於減肥的人非常重要的功能~~

每天提供線型圖~~ 告訴你~~ 你的體重~~ 是在增加~~還是在減少~~

並且也可以放在BLOG上面~~ 大家就去參考一下吧~~~~

Write a simple Windows SideShow program

筆記型電腦上的 Windows SideShow

Windows SideShow is a new technology in Windows Vista that supports a secondary screen on your mobile PC. With this additional display you can view important information whether your laptop is on, off, or in sleep mode. Windows SideShow is available in Windows Vista Home Premium, Windows Vista Business, Windows Vista Enterprise, and Windows Vista Ultimate. For more detail, please refer http://www.microsoft.com/windows/products/windowsvista/features/details/sideshow.mspx

For Engineer to development a simple SideShow program:

  1. Make sure you using Vista RTM
  2. Download SideShow emulator: http://www.microsoft.com/downloads/details.aspx?FamilyID=bba99eb2-aa6d-4133-b433-933a2c4d41dc&displaylang=en
  3. Install VS2005.
  4. Install Windows Vista SDK and .NET (http://www.microsoft.com/downloads/details.aspx?FamilyId=C2B1E300-F358-4523-B479-F53D234CDCCF&displaylang=en)
  5. Open solution: C:Program FilesMicrosoft SDKsWindowsv6.0SampleswinuisideshowHelloWorldHelloWorld.sln
  6. Build it
  7. Run registery “HelloWorld.reg”
  8. Run bin file in C:Program FilesMicrosoft SDKsWindowsv6.0SampleswinuisideshowHelloWorldDebugWindowsSideShowHelloWorld.exe
  9. Start Emulator, you should show “Hello World” here.~~~

Windows SideShow 頁面

Write simple WMI program


(Diagram: 淺談CIM與WMI-名詞介紹)

WMI (Windows Management Instrumentation) provide a kind of easy way to let you query data from your system (Ex: OS version, share folder name … etc).

It is easy to understand, using WMI to query information is the same with query data from database. You can just using “SELECT * from Win32_Process” to find out all exist process in your computer.

There are some examples which describe how to create it using C# or C++ code.

  1. Example: Creating a WMI Application(C++)

  2. WMI Sample  (using C#)

There are serveral extension reading..

Here is sample code of C#

using System.Management; //This is equivalent to "SELECT * FROM Win32_Service" SelectQuery sysSQL = new SelectQuery("Win32_bios"); > > MnagementObjectSearcher sysRet =    new ManagementObjectSearcher(sysSQL); > > // Display each entry for Win32_bios foreach (ManagementObject sysInfo in sysRet.Get()) {   this.textBox1.Text = "Bios version: " + sysInfo["version"].ToString(); } > >