2012年8月15日 星期三

在十六進位字串和數字型別間轉換


本主題中的範例會示範如何執行下列工作:
  • 取得 string 中每個字元的十六進位值。
  • 取得對應至十六進位 string 中每個值的 char
  • 將十六進位 string 轉換為 int
  • 將十六進位 string 轉換為 float
  • 將 byte 陣列轉換為十六進位 string
這個範例會輸出 string 中每個字元的十六進位值。 首先,會將 string 剖析至字元陣列。 然後,會呼叫每個字元上的 ToInt32(Char) 取得其數值。 最後,它會在 string 中將數字格式化為十六進位表示。
string input = "Hello World!";
char[] values = input.ToCharArray();
foreach (char letter in values)
{
    // Get the integral value of the character.
    int value = Convert.ToInt32(letter);
    // Convert the decimal value to a hexadecimal value in string form.
    string hexOutput = String.Format("{0:X}", value);
    Console.WriteLine("Hexadecimal value of {0} is {1}", letter, hexOutput);
}
/* Output:
   Hexadecimal value of H is 48
    Hexadecimal value of e is 65
    Hexadecimal value of l is 6C
    Hexadecimal value of l is 6C
    Hexadecimal value of o is 6F
    Hexadecimal value of   is 20
    Hexadecimal value of W is 57
    Hexadecimal value of o is 6F
    Hexadecimal value of r is 72
    Hexadecimal value of l is 6C
    Hexadecimal value of d is 64
    Hexadecimal value of ! is 21
 */


這個範例會剖析十六進位值的 string,並輸出對應至每個十六進位值的字元。 首先,呼叫 Split(Char[]) 方法取得每個十六進位值,做為陣列中的個別 string 然後呼叫ToInt32(String, Int32),將十六進位值轉換為以 int 表示的十進位值。 會顯示兩種不同的方法,以取得對應至該字元程式碼的字元。 第一個技術會使用 ConvertFromUtf32(Int32),傳回對應至整數引數的字元做為 string 第二個技術會明確將 int 轉型為 char
string hexValues = "48 65 6C 6C 6F 20 57 6F 72 6C 64 21";
string[] hexValuesSplit = hexValues.Split(' ');
foreach (String hex in hexValuesSplit)
{
    // Convert the number expressed in base-16 to an integer.
    int value = Convert.ToInt32(hex, 16);
    // Get the character corresponding to the integral value.
    string stringValue = Char.ConvertFromUtf32(value);
    char charValue = (char)value;
    Console.WriteLine("hexadecimal value = {0}, int value = {1}, char value = {2} or {3}",
                        hex, value, stringValue, charValue);
}
/* Output:
    hexadecimal value = 48, int value = 72, char value = H or H
    hexadecimal value = 65, int value = 101, char value = e or e
    hexadecimal value = 6C, int value = 108, char value = l or l
    hexadecimal value = 6C, int value = 108, char value = l or l
    hexadecimal value = 6F, int value = 111, char value = o or o
    hexadecimal value = 20, int value = 32, char value =   or
    hexadecimal value = 57, int value = 87, char value = W or W
    hexadecimal value = 6F, int value = 111, char value = o or o
    hexadecimal value = 72, int value = 114, char value = r or r
    hexadecimal value = 6C, int value = 108, char value = l or l
    hexadecimal value = 64, int value = 100, char value = d or d
    hexadecimal value = 21, int value = 33, char value = ! or !
*/


這個範例會示範另一個方法,經由呼叫 Parse(String, NumberStyles) 方法,將十六進位 string 轉換為整數。
string hexString = "8E2";
int num = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
Console.WriteLine(num);
//Output: 2274


下列範例會示範如何使用 System.BitConverter 類別和 Int32.Parse 方法,將十六進位 string 轉換為float
string hexString = "43480170";
uint num = uint.Parse(hexString, System.Globalization.NumberStyles.AllowHexSpecifier);

byte[] floatVals = BitConverter.GetBytes(num);
float f = BitConverter.ToSingle(floatVals, 0);
Console.WriteLine("float convert = {0}", f);

// Output: 200.0056            


下列範例會示範如何使用 System.BitConverter 類別,將 byte 陣列轉換為十六進位字串。
byte[] vals = { 0x01, 0xAA, 0xB1, 0xDC, 0x10, 0xDD };

string str = BitConverter.ToString(vals);
Console.WriteLine(str);

str = BitConverter.ToString(vals).Replace("-", "");
Console.WriteLine(str);

/*Output:
  01-AA-B1-DC-10-DD
  01AAB1DC10DD
 */


沒有留言:

張貼留言