2017年1月2日 星期一

C# 二進制字串轉數值

範例:將字串 "1000000000000000000" 轉成 int 數值
  1.  
  2. using System;
  3. using System.Text.RegularExpressions;
  4. namespace BinaryStringToInteger
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. BinaryStringToInteger("1000000000000000000");
  11. Console.ReadKey();
  12. }
  13. static readonly Regex Binary = new Regex("^[01]{1,32}$", RegexOptions.Compiled);
  14. static void BinaryStringToInteger(string s)
  15. {
  16. if (Binary.IsMatch(s))
  17. {
  18. Console.WriteLine(Convert.ToInt32(s, 2));
  19. }
  20. else
  21. {
  22. Console.WriteLine("invalid: " + s);
  23. }
  24. }
  25. }
  26. }
  27.  
  28.  

沒有留言:

Visual Studio 2017/2019 推薦的擴充功能與更新

參考文章: 覺得 Google 的 Blogger 不太順手?透過 HTML 的 iframe 移花接木 HackMD