2011年11月24日 星期四

字串轉10進位,16進位

參考:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Console
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //假設特定字串如下
  13. string subject = "??  國文(一) Web Development 網路發展";
  14. char[] charArr = subject.ToCharArray();
  15.  
  16. for (int counter = 0; counter < charArr.Length; counter++)
  17. {
  18. int intValue = Convert.ToInt32(charArr[counter]);
  19. string strHex = String.Format("{0:X}", intValue);
  20. System.Console.WriteLine("字元: {0}, 十進位: {1}, 十六進位: {2}", charArr[counter].ToString(), intValue, strHex);
  21. }
  22.  
  23. System.Console.Read();
  24. }
  25. }
  26. }
--
我自己練習:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As String = "1234這78"
        Dim b As Char() = a.ToCharArray
        Dim intvalue As Integer
        Dim hexstr As String
        ListBox1.Items.Clear()
        For i As Integer = 0 To b.Length - 1
            intvalue = Convert.ToInt32(b(i))
            hexstr = String.Format("{0:x}", intvalue)
            ListBox1.Items.Add(String.Format("值:{0},十進位:{1},十六進位:{2}", b(i).ToString, intvalue, hexstr))
        Next

    End Sub
End Class

沒有留言:

張貼留言