C# 中的 Convert.ToByte 方法


Convert.ToByte 方法用於將指定值轉換為 8 位無符號整數。

假設我們有一個 char 變數。

Char charVal = ‘a’;

現在,將其轉換為 8 位無符號整數。

byte byteVal = Convert.ToByte(charVal);

現在,讓我們看另一個示例。

示例

 線上演示

using System;
public class Demo {
   public static void Main() {
      char[] charVal = { 'p', 'q', 'r', 's' };
      foreach (char c in charVal) {
         byte byteVal = Convert.ToByte(c);
         Console.WriteLine("{0} is converted to = {1}", c, byteVal);
      }
   }
}

輸出

p is converted to = 112
q is converted to = 113
r is converted to = 114
s is converted to = 115

更新於:2020 年 6 月 23 日

956 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始吧
廣告
© . All rights reserved.