Convert.ToByte Yöntem

Tanım

Belirtilen değeri 8 bit işaretsiz tamsayıya dönüştürür.

Aşırı Yüklemeler

ToByte(String)

Bir sayının belirtilen dize gösterimini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

ToByte(UInt16)

Belirtilen 16 bit işaretsiz tamsayı değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

ToByte(UInt32)

Belirtilen 32 bit işaretsiz tamsayı değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

ToByte(String, Int32)

Belirtilen tabandaki bir sayının dize gösterimini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

ToByte(Object, IFormatProvider)

Belirtilen kültüre özgü biçimlendirme bilgilerini kullanarak belirtilen nesnenin değerini 8 bit işaretsiz tamsayıya dönüştürür.

ToByte(String, IFormatProvider)

Bir sayının belirtilen dize gösterimini, belirtilen kültüre özgü biçimlendirme bilgilerini kullanarak eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

ToByte(Single)

Belirtilen tek duyarlıklı kayan noktalı sayının değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

ToByte(UInt64)

Belirtilen 64 bit işaretsiz tamsayı değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

ToByte(SByte)

Belirtilen 8 bit işaretli tamsayı değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

ToByte(DateTime)

Bu yöntemin çağrılması her zaman oluşturur InvalidCastException.

ToByte(Int64)

Belirtilen 64 bit işaretli tamsayı değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

ToByte(Byte)

Belirtilen 8 bit işaretsiz tamsayıyı döndürür; gerçek dönüştürme gerçekleştirilmemesi.

ToByte(Char)

Belirtilen Unicode karakterinin değerini eşdeğer 8 bit işaretsiz tamsayıya dönüştürür.

ToByte(Decimal)

Belirtilen ondalık sayının değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

ToByte(Boolean)

Belirtilen Boole değerini eşdeğer 8 bit işaretsiz tamsayıya dönüştürür.

ToByte(Int16)

Belirtilen 16 bit işaretli tamsayı değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

ToByte(Int32)

Belirtilen 32 bit işaretli tamsayı değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

ToByte(Double)

Belirtilen çift duyarlıklı kayan noktalı sayının değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

ToByte(Object)

Belirtilen nesnenin değerini 8 bit işaretsiz tamsayıya dönüştürür.

ToByte(String)

Kaynak:
Convert.cs
Kaynak:
Convert.cs
Kaynak:
Convert.cs

Bir sayının belirtilen dize gösterimini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

C#
public static byte ToByte (string value);
C#
public static byte ToByte (string? value);

Parametreler

value
String

Dönüştürülecek sayıyı içeren bir dize.

Döndürülenler

ile eşdeğer valuebir 8 bit işaretsiz tamsayı veya ise value sıfır.null

Özel durumlar

value , isteğe bağlı bir işaretin ardından bir basamak dizisi (0 ile 9 arasında) arasında bir işaretten oluşmaz.

value Byte.MinValue değerinden küçük veya Byte.MaxValue değerinden büyük bir sayıyı temsil eder.

Örnekler

Aşağıdaki örnek bir dize dizisini tanımlar ve her dizeyi öğesine Bytedönüştürmeye çalışır. Bir null dize sıfıra ayrıştırırken bir String.EmptyFormatExceptionoluşturduğunu unutmayın. Ayrıca, baştaki ve sondaki boşluklar başarıyla ayrıştırılsa da para birimi simgeleri, grup ayırıcıları veya ondalık ayırıcılar gibi biçimlendirme simgelerinin ayrıştırılmadığını unutmayın.

C#
using System;

public class Example
{
   public static void Main()
   {
      String[] values = { null, "", "0xC9", "C9", "101", "16.3",
                          "$12", "$12.01", "-4", "1,032", "255",
                          "   16  " };
      foreach (var value in values) {
         try {
            byte number = Convert.ToByte(value);
            Console.WriteLine("'{0}' --> {1}",
                              value == null ? "<null>" : value, number);
         }
         catch (FormatException) {
            Console.WriteLine("Bad Format: '{0}'",
                              value == null ? "<null>" : value);
         }
         catch (OverflowException) {
            Console.WriteLine("OverflowException: '{0}'", value);
         }
      }
   }
}
// The example displays the following output:
//     '<null>' --> 0
//     Bad Format: ''
//     Bad Format: '0xC9'
//     Bad Format: 'C9'
//     '101' --> 101
//     Bad Format: '16.3'
//     Bad Format: '$12'
//     Bad Format: '$12.01'
//     OverflowException: '-4'
//     Bad Format: '1,032'
//     '255' --> 255
//     '   16  ' --> 16

Açıklamalar

yönteminin ToByte(String) kullanılması yöntemine geçirmekle valueByte.Parse(String) eşdeğerdir. value geçerli kültürün biçimlendirme kuralları kullanılarak yorumlanır.

Dönüştürme başarısız olursa bir özel durumu işlemeyi tercih ederseniz, bunun yerine yöntemini çağırabilirsiniz Byte.TryParse . Dönüştürmenin başarılı mı yoksa başarısız mı olduğunu gösteren bir Boolean değer döndürür.

Ayrıca bkz.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToByte(UInt16)

Kaynak:
Convert.cs
Kaynak:
Convert.cs
Kaynak:
Convert.cs

Önemli

Bu API, CLS uyumlu değildir.

Belirtilen 16 bit işaretsiz tamsayı değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

C#
[System.CLSCompliant(false)]
public static byte ToByte (ushort value);

Parametreler

value
UInt16

Dönüştürülecek 16 bitlik işaretsiz tamsayı.

Döndürülenler

ile eşdeğer valuebir 8 bit işaretsiz tamsayı.

Öznitelikler

Özel durumlar

value , Byte.MaxValue değerinden büyüktür.

Örnekler

Aşağıdaki örnek, işaretsiz 16 bitlik tamsayı dizisini değerlere Byte dönüştürür.

C#
ushort[] numbers = { UInt16.MinValue, 121, 340, UInt16.MaxValue };
byte result;
foreach (ushort number in numbers)
{
   try {
      result = Convert.ToByte(number);
      Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                        number.GetType().Name, number,
                        result.GetType().Name, result);
   }
   catch (OverflowException) {
      Console.WriteLine("The {0} value {1} is outside the range of the Byte type.",
                        number.GetType().Name, number);
   }
}
// The example displays the following output:
//       Converted the UInt16 value 0 to the Byte value 0.
//       Converted the UInt16 value 121 to the Byte value 121.
//       The UInt16 value 340 is outside the range of the Byte type.
//       The UInt16 value 65535 is outside the range of the Byte type.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToByte(UInt32)

Kaynak:
Convert.cs
Kaynak:
Convert.cs
Kaynak:
Convert.cs

Önemli

Bu API, CLS uyumlu değildir.

Belirtilen 32 bit işaretsiz tamsayı değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

C#
[System.CLSCompliant(false)]
public static byte ToByte (uint value);

Parametreler

value
UInt32

Dönüştürülecek 32 bit işaretsiz tamsayı.

Döndürülenler

ile eşdeğer valuebir 8 bit işaretsiz tamsayı.

Öznitelikler

Özel durumlar

value , Byte.MaxValue değerinden büyüktür.

Örnekler

Aşağıdaki örnek, işaretsiz tamsayı dizisini değerlere Byte dönüştürür.

C#
uint[] numbers = { UInt32.MinValue, 121, 340, UInt32.MaxValue };
byte result;
foreach (uint number in numbers)
{
   try {
      result = Convert.ToByte(number);
      Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                        number.GetType().Name, number,
                        result.GetType().Name, result);
   }
   catch (OverflowException) {
      Console.WriteLine("The {0} value {1} is outside the range of the Byte type.",
                        number.GetType().Name, number);
   }
}
// The example displays the following output:
//       Converted the UInt32 value 0 to the Byte value 0.
//       Converted the UInt32 value 121 to the Byte value 121.
//       The UInt32 value 340 is outside the range of the Byte type.
//       The UInt32 value 4294967295 is outside the range of the Byte type.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToByte(String, Int32)

Kaynak:
Convert.cs
Kaynak:
Convert.cs
Kaynak:
Convert.cs

Belirtilen tabandaki bir sayının dize gösterimini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

C#
public static byte ToByte (string value, int fromBase);
C#
public static byte ToByte (string? value, int fromBase);

Parametreler

value
String

Dönüştürülecek sayıyı içeren bir dize.

fromBase
Int32

içindeki value2, 8, 10 veya 16 olması gereken sayının tabanı.

Döndürülenler

içindeki sayıya valueeşdeğer bir 8 bit işaretsiz tamsayı veya ise valuenull0 (sıfır)

Özel durumlar

fromBase 2, 8, 10 veya 16 değildir.

-veya-

value, temel 10 imzasız bir sayıyı temsil eder, negatif bir işaret ön ekli.

value, Empty değeridir.

value tarafından fromBasebelirtilen tabanda geçerli bir basamak olmayan bir karakter içerir. Özel durum iletisi, içindeki value ilk karakter geçersizse dönüştürülecek basamak olmadığını gösterir; aksi takdirde ileti, sonunda geçersiz karakterler olduğunu value gösterir.

value, 10 tabanında imzalanmamış bir sayıyı temsil eder ve önüne negatif bir işaret eklenir.

-veya-

value Byte.MinValue değerinden küçük veya Byte.MaxValue değerinden büyük bir sayıyı temsil eder.

Örnekler

Aşağıdaki örnek alternatif olarak bir dize dizisini ikili, sekizli, ondalık ve onaltılık değerlerin gösterimi olarak yorumlamaya çalışır.

C#
using System;

public class Example
{
   public static void Main()
   {
      int[] bases = { 2, 8, 10, 16 };
      string[] values = { "-1", "1", "08", "0F", "11" , "12", "30",
                          "101", "255", "FF", "10000000", "80" };
      byte number;
      foreach (int numBase in bases)
      {
         Console.WriteLine("Base {0}:", numBase);
         foreach (string value in values)
         {
            try {
               number = Convert.ToByte(value, numBase);
               Console.WriteLine("   Converted '{0}' to {1}.", value, number);
            }
            catch (FormatException) {
               Console.WriteLine("   '{0}' is not in the correct format for a base {1} byte value.",
                                 value, numBase);
            }
            catch (OverflowException) {
               Console.WriteLine("   '{0}' is outside the range of the Byte type.", value);
            }
            catch (ArgumentException) {
               Console.WriteLine("   '{0}' is invalid in base {1}.", value, numBase);
            }
         }
      }
   }
}
// The example displays the following output:
//    Base 2:
//       '-1' is invalid in base 2.
//       Converted '1' to 1.
//       '08' is not in the correct format for a base 2 conversion.
//       '0F' is not in the correct format for a base 2 conversion.
//       Converted '11' to 3.
//       '12' is not in the correct format for a base 2 conversion.
//       '30' is not in the correct format for a base 2 conversion.
//       Converted '101' to 5.
//       '255' is not in the correct format for a base 2 conversion.
//       'FF' is not in the correct format for a base 2 conversion.
//       Converted '10000000' to 128.
//       '80' is not in the correct format for a base 2 conversion.
//    Base 8:
//       '-1' is invalid in base 8.
//       Converted '1' to 1.
//       '08' is not in the correct format for a base 8 conversion.
//       '0F' is not in the correct format for a base 8 conversion.
//       Converted '11' to 9.
//       Converted '12' to 10.
//       Converted '30' to 24.
//       Converted '101' to 65.
//       Converted '255' to 173.
//       'FF' is not in the correct format for a base 8 conversion.
//       '10000000' is outside the range of the Byte type.
//       '80' is not in the correct format for a base 8 conversion.
//    Base 10:
//       '-1' is outside the range of the Byte type.
//       Converted '1' to 1.
//       Converted '08' to 8.
//       '0F' is not in the correct format for a base 10 conversion.
//       Converted '11' to 11.
//       Converted '12' to 12.
//       Converted '30' to 30.
//       Converted '101' to 101.
//       Converted '255' to 255.
//       'FF' is not in the correct format for a base 10 conversion.
//       '10000000' is outside the range of the Byte type.
//       Converted '80' to 80.
//    Base 16:
//       '-1' is invalid in base 16.
//       Converted '1' to 1.
//       Converted '08' to 8.
//       Converted '0F' to 15.
//       Converted '11' to 17.
//       Converted '12' to 18.
//       Converted '30' to 48.
//       '101' is outside the range of the Byte type.
//       '255' is outside the range of the Byte type.
//       Converted 'FF' to 255.
//       '10000000' is outside the range of the Byte type.
//       Converted '80' to 128.

Açıklamalar

16 ise fromBase , parametresi tarafından belirtilen sayıya value "0x" veya "0X" ön eki ekleyebilirsiniz.

Byte Veri türü yalnızca imzasız değerleri desteklediğindenToByte(String, Int32), yöntemi bunun value imzasız ikili gösterim kullanılarak ifade edildiği varsayılır. Başka bir deyişle, sekiz bitin tümü sayısal değeri temsil etmek için kullanılır ve bir işaret biti yoktur. Sonuç olarak, yöntem özel durum oluşturmadan veri türü aralığının dışında olan imzalı bir bayt değerinin Byte bir Byte değere dönüştürüldüğü kodu yazmak mümkündür. Aşağıdaki örnek onaltılık dize gösterimine dönüştürür MinValue ve yöntemini çağırır ToByte(String, Int32) . yöntemi özel durum atmak yerine "0x80 128'e dönüştürür" iletisini görüntüler.

C#
// Create a hexadecimal value out of range of the Byte type.
string value = SByte.MinValue.ToString("X");
// Convert it back to a number.
try
{
   byte number = Convert.ToByte(value, 16);
   Console.WriteLine("0x{0} converts to {1}.", value, number);
}
catch (OverflowException)
{
   Console.WriteLine("Unable to convert '0x{0}' to a byte.", value);
}

İkili işlemler veya sayısal dönüştürme işlemlerini gerçekleştirirken, bir yöntem veya işlecin belirli bir değeri yorumlamak için uygun sayısal gösterimi kullandığını doğrulamak her zaman geliştiricinin sorumluluğundadır. Aşağıdaki örnekte, yöntemin onaltılık dize gösterimini bir değere dönüştürdüğünde uygun olmayan imzasız ikili gösterimi kullanmamasını sağlamaya yönelik bir Byte teknik gösterilmektedir. Örnek, dize gösterimine dönüştürme sırasında bir değerin bir işaretli veya işaretsiz bir tamsayı temsil edip etmediğini belirler. Örnek değeri bir Byte değere geri dönüştürdüğünde, özgün değerin imzalı bir tamsayı olup olmadığını denetler. Bu durumda ve yüksek sıralı biti ayarlanırsa (değerin negatif olduğunu ve imzalanmamış ikili gösterim yerine ikinin tamamlayıcısını kullandığını gösterir), yöntem bir özel durum oluşturur.

C#
// Create a negative hexadecimal value out of range of the Byte type.
sbyte sourceNumber = SByte.MinValue;
bool isSigned = Math.Sign((sbyte)sourceNumber.GetType().GetField("MinValue").GetValue(null)) == -1;
string value = sourceNumber.ToString("X");
byte targetNumber;
try
{
   targetNumber = Convert.ToByte(value, 16);
   if (isSigned && ((targetNumber & 0x80) != 0))
      throw new OverflowException();
   else
      Console.WriteLine("0x{0} converts to {1}.", value, targetNumber);
}
catch (OverflowException)
{
   Console.WriteLine("Unable to convert '0x{0}' to an unsigned byte.", value);
}
// Displays the following to the console:
//    Unable to convert '0x80' to an unsigned byte.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToByte(Object, IFormatProvider)

Kaynak:
Convert.cs
Kaynak:
Convert.cs
Kaynak:
Convert.cs

Belirtilen kültüre özgü biçimlendirme bilgilerini kullanarak belirtilen nesnenin değerini 8 bit işaretsiz tamsayıya dönüştürür.

C#
public static byte ToByte (object value, IFormatProvider provider);
C#
public static byte ToByte (object? value, IFormatProvider? provider);

Parametreler

value
Object

Arabirimini uygulayan IConvertible bir nesne.

provider
IFormatProvider

Kültüre özgü biçimlendirme bilgileri sağlayan nesne.

Döndürülenler

ile eşdeğer valuebir 8 bit işaretsiz tamsayı veya ise value sıfır.null

Özel durumlar

value bir değerin özellik biçiminde Byte değil.

value uygulamaz IConvertible.

-veya-

türünden value türüne Byte dönüştürme desteklenmez.

value Byte.MinValue değerinden küçük veya Byte.MaxValue değerinden büyük bir sayıyı temsil eder.

Örnekler

Aşağıdaki örnek, arabirimini uygulayan IConvertible bir ByteString sınıfı tanımlar. sınıfı, hem imzalı hem de imzasız bayt değerlerini temsil edebilmesi için bayt değerinin dize gösterimini bir işaret alanıyla birlikte depolar.

C#
using System;
using System.Globalization;

public enum SignBit { Negative=-1, Zero=0, Positive=1 };

public struct ByteString : IConvertible
{
   private SignBit signBit;
   private string byteString;

   public SignBit Sign
   {
      set { signBit = value; }
      get { return signBit; }
   }

   public string Value
   {
      set {
         if (value.Trim().Length > 2)
            throw new ArgumentException("The string representation of a byte cannot have more than two characters.");
         else
            byteString = value;
      }
      get { return byteString; }
   }

   // IConvertible implementations.
   public TypeCode GetTypeCode() {
      return TypeCode.Object;
   }

   public bool ToBoolean(IFormatProvider provider)
   {
      if (signBit == SignBit.Zero)
         return false;
      else
         return true;
   }

   public byte ToByte(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         throw new OverflowException(String.Format("{0} is out of range of the Byte type.", Convert.ToSByte(byteString, 16)));
      else
         return Byte.Parse(byteString, NumberStyles.HexNumber);
   }

   public char ToChar(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative) {
         throw new OverflowException(String.Format("{0} is out of range of the Char type.", Convert.ToSByte(byteString, 16)));
      }
      else {
         byte byteValue = Byte.Parse(this.byteString, NumberStyles.HexNumber);
         return Convert.ToChar(byteValue);
      }
   }

   public DateTime ToDateTime(IFormatProvider provider)
   {
      throw new InvalidCastException("ByteString to DateTime conversion is not supported.");
   }

   public decimal ToDecimal(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
      {
         sbyte byteValue = SByte.Parse(byteString, NumberStyles.HexNumber);
         return Convert.ToDecimal(byteValue);
      }
      else
      {
         byte byteValue = Byte.Parse(byteString, NumberStyles.HexNumber);
         return Convert.ToDecimal(byteValue);
      }
   }

   public double ToDouble(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         return Convert.ToDouble(SByte.Parse(byteString, NumberStyles.HexNumber));
      else
         return Convert.ToDouble(Byte.Parse(byteString, NumberStyles.HexNumber));
   }

   public short ToInt16(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         return Convert.ToInt16(SByte.Parse(byteString, NumberStyles.HexNumber));
      else
         return Convert.ToInt16(Byte.Parse(byteString, NumberStyles.HexNumber));
   }

   public int ToInt32(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         return Convert.ToInt32(SByte.Parse(byteString, NumberStyles.HexNumber));
      else
         return Convert.ToInt32(Byte.Parse(byteString, NumberStyles.HexNumber));
   }

   public long ToInt64(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         return Convert.ToInt64(SByte.Parse(byteString, NumberStyles.HexNumber));
      else
         return Convert.ToInt64(Byte.Parse(byteString, NumberStyles.HexNumber));
   }

   public sbyte ToSByte(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         try {
            return Convert.ToSByte(Byte.Parse(byteString, NumberStyles.HexNumber));
         }
         catch (OverflowException e) {
            throw new OverflowException(String.Format("{0} is outside the range of the SByte type.",
                                                   Byte.Parse(byteString, NumberStyles.HexNumber)), e);
         }
      else
         return SByte.Parse(byteString, NumberStyles.HexNumber);
   }

   public float ToSingle(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         return Convert.ToSingle(SByte.Parse(byteString, NumberStyles.HexNumber));
      else
         return Convert.ToSingle(Byte.Parse(byteString, NumberStyles.HexNumber));
   }

   public string ToString(IFormatProvider provider)
   {
      return "0x" + this.byteString;
   }

   public object ToType(Type conversionType, IFormatProvider provider)
   {
      switch (Type.GetTypeCode(conversionType))
      {
         case TypeCode.Boolean:
            return this.ToBoolean(null);
         case TypeCode.Byte:
            return this.ToByte(null);
         case TypeCode.Char:
            return this.ToChar(null);
         case TypeCode.DateTime:
            return this.ToDateTime(null);
         case TypeCode.Decimal:
            return this.ToDecimal(null);
         case TypeCode.Double:
            return this.ToDouble(null);
         case TypeCode.Int16:
            return this.ToInt16(null);
         case TypeCode.Int32:
            return this.ToInt32(null);
         case TypeCode.Int64:
            return this.ToInt64(null);
         case TypeCode.Object:
            if (typeof(ByteString).Equals(conversionType))
               return this;
            else
               throw new InvalidCastException(String.Format("Conversion to a {0} is not supported.", conversionType.Name));
         case TypeCode.SByte:
            return this.ToSByte(null);
         case TypeCode.Single:
            return this.ToSingle(null);
         case TypeCode.String:
            return this.ToString(null);
         case TypeCode.UInt16:
            return this.ToUInt16(null);
         case TypeCode.UInt32:
            return this.ToUInt32(null);
         case TypeCode.UInt64:
            return this.ToUInt64(null);
         default:
            throw new InvalidCastException(String.Format("Conversion to {0} is not supported.", conversionType.Name));
      }
   }

   public UInt16 ToUInt16(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         throw new OverflowException(String.Format("{0} is outside the range of the UInt16 type.",
                                                   SByte.Parse(byteString, NumberStyles.HexNumber)));
      else
         return Convert.ToUInt16(Byte.Parse(byteString, NumberStyles.HexNumber));
   }

   public UInt32 ToUInt32(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         throw new OverflowException(String.Format("{0} is outside the range of the UInt32 type.",
                                                   SByte.Parse(byteString, NumberStyles.HexNumber)));
      else
         return Convert.ToUInt32(Byte.Parse(byteString, NumberStyles.HexNumber));
   }

   public UInt64 ToUInt64(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         throw new OverflowException(String.Format("{0} is outside the range of the UInt64 type.",
                                                   SByte.Parse(byteString, NumberStyles.HexNumber)));
      else
         return Convert.ToUInt64(Byte.Parse(byteString, NumberStyles.HexNumber));
   }
}

Aşağıdaki örnek birkaç ByteString nesnenin örneğini oluşturur ve bunları bayt değerlerine dönüştürmek için yöntemini çağırır ToByte(Object, IFormatProvider) . Yönteminin ToByte(Object, IFormatProvider) dönüştürülecek nesnenin IConvertible.ToByte yöntemine bir çağrı sarmalandığını gösterir.

C#
public class Class1
{
   public static void Main()
   {
      byte positiveByte = 216;
      sbyte negativeByte = -101;

      ByteString positiveString = new ByteString();
      positiveString.Sign = (SignBit) Math.Sign(positiveByte);
      positiveString.Value = positiveByte.ToString("X2");

      ByteString negativeString = new ByteString();
      negativeString.Sign = (SignBit) Math.Sign(negativeByte);
      negativeString.Value = negativeByte.ToString("X2");

      try {
         Console.WriteLine("'{0}' converts to {1}.", positiveString.Value, Convert.ToByte(positiveString));
      }
      catch (OverflowException) {
         Console.WriteLine("0x{0} is outside the range of the Byte type.", positiveString.Value);
      }

      try {
         Console.WriteLine("'{0}' converts to {1}.", negativeString.Value, Convert.ToByte(negativeString));
      }
      catch (OverflowException) {
         Console.WriteLine("0x{0} is outside the range of the Byte type.", negativeString.Value);
      }
   }
}
// The example displays the following output:
//       'D8' converts to 216.
//       0x9B is outside the range of the Byte type.

Açıklamalar

provider kullanıcının içeriği hakkında kültüre özgü dönüştürme bilgilerini belirtmesini valuesağlar. Temel türler yoksayarprovider; ancak arabirimini uygulayan IConvertible kullanıcı tanımlı bir türse value parametresi kullanılabilir.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToByte(String, IFormatProvider)

Kaynak:
Convert.cs
Kaynak:
Convert.cs
Kaynak:
Convert.cs

Bir sayının belirtilen dize gösterimini, belirtilen kültüre özgü biçimlendirme bilgilerini kullanarak eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

C#
public static byte ToByte (string value, IFormatProvider provider);
C#
public static byte ToByte (string? value, IFormatProvider? provider);

Parametreler

value
String

Dönüştürülecek sayıyı içeren bir dize.

provider
IFormatProvider

Kültüre özgü biçimlendirme bilgileri sağlayan nesne.

Döndürülenler

ile eşdeğer valuebir 8 bit işaretsiz tamsayı veya ise value sıfır.null

Özel durumlar

value , isteğe bağlı bir işaretin ardından bir basamak dizisi (0 ile 9 arasında) arasında bir işaretten oluşmaz.

value Byte.MinValue değerinden küçük veya Byte.MaxValue değerinden büyük bir sayıyı temsil eder.

Örnekler

Aşağıdaki örnek, pozitif işareti "pos" ve negatif işareti de yöntemine yapılan çağrılarda ToByte(String, IFormatProvider) kullandığı "neg" olarak tanımlayan özel NumberFormatInfo bir nesne oluşturur. Daha sonra bir dize dizisindeki ToByte(String, IFormatProvider) her öğeyi bir Byte değere dönüştürmek için yöntemini tekrar tekrar çağırır.

C#
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      // Create a NumberFormatInfo object and set several of its
      // properties that apply to unsigned bytes.
      NumberFormatInfo provider = new NumberFormatInfo();

      // These properties affect the conversion.
      provider.PositiveSign = "pos ";
      provider.NegativeSign = "neg ";

      // This property does not affect the conversion.
      // The input string cannot have a decimal separator.
      provider.NumberDecimalSeparator = ".";

      // Define an array of numeric strings.
      string[] numericStrings = { "234", "+234", "pos 234", "234.", "255",
                                  "256", "-1" };

      foreach (string numericString in numericStrings)
      {
         Console.Write("'{0,-8}' ->   ", numericString);
         try {
            byte number = Convert.ToByte(numericString, provider);
            Console.WriteLine(number);
         }
         catch (FormatException) {
            Console.WriteLine("Incorrect Format");
         }
         catch (OverflowException) {
            Console.WriteLine("Overflows a Byte");
         }
      }
   }
}
// The example displays the following output:
//       '234     ' ->   234
//       '+234    ' ->   Incorrect Format
//       'pos 234 ' ->   234
//       '234.    ' ->   Incorrect Format
//       '255     ' ->   255
//       '256     ' ->   Overflows a Byte
//       '-1      ' ->   Incorrect Format

Açıklamalar

provider bir nesnesi edinen bir IFormatProviderNumberFormatInfo örnektir. NumberFormatInfo nesnesi, biçimi valuehakkında kültüre özgü bilgiler sağlar. ise providernull, NumberFormatInfo geçerli kültürün nesnesi kullanılır.

Dönüştürme başarısız olursa bir özel durumu işlemeyi tercih ederseniz, bunun yerine yöntemini çağırabilirsiniz Byte.TryParse . Dönüştürmenin başarılı mı yoksa başarısız mı olduğunu gösteren bir Boolean değer döndürür.

Ayrıca bkz.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToByte(Single)

Kaynak:
Convert.cs
Kaynak:
Convert.cs
Kaynak:
Convert.cs

Belirtilen tek duyarlıklı kayan noktalı sayının değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

C#
public static byte ToByte (float value);

Parametreler

value
Single

Bir tek duyarlıklı kayan noktalı sayı.

Döndürülenler

value, en yakın 8 bit işaretsiz tamsayıya yuvarlanmış. İki tamsayının ortasındaysa value çift sayı döndürülür; yani 4,5 4'e, 5,5 ise 6'ya dönüştürülür.

Özel durumlar

value , Byte.MaxValue değerinden büyük veya Byte.MinValue değerinden küçüktür.

Örnekler

Aşağıdaki örnek bir Single değeri Bytedeğerine dönüştürür.

C#
public void ConvertByteSingle(byte byteVal) {
    float floatVal;

    // Byte to float conversion will not overflow.
    floatVal = System.Convert.ToSingle(byteVal);
    System.Console.WriteLine("The byte as a float is {0}.",
        floatVal);

    // Float to byte conversion can overflow.
    try {
        byteVal = System.Convert.ToByte(floatVal);
        System.Console.WriteLine("The float as a byte is {0}.",
            byteVal);
    }
    catch (System.OverflowException) {
        System.Console.WriteLine(
            "The float value is too large for a byte.");
    }
}

Ayrıca bkz.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToByte(UInt64)

Kaynak:
Convert.cs
Kaynak:
Convert.cs
Kaynak:
Convert.cs

Önemli

Bu API, CLS uyumlu değildir.

Belirtilen 64 bit işaretsiz tamsayı değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

C#
[System.CLSCompliant(false)]
public static byte ToByte (ulong value);

Parametreler

value
UInt64

Dönüştürülecek 64 bit işaretsiz tamsayı.

Döndürülenler

ile eşdeğer valuebir 8 bit işaretsiz tamsayı.

Öznitelikler

Özel durumlar

value , Byte.MaxValue değerinden büyüktür.

Örnekler

Aşağıdaki örnek, işaretsiz uzun tamsayı dizisini değerlere Byte dönüştürür.

C#
ulong[] numbers= { UInt64.MinValue, 121, 340, UInt64.MaxValue };
byte result;
foreach (ulong number in numbers)
{
   try {
      result = Convert.ToByte(number);
      Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                        number.GetType().Name, number,
                        result.GetType().Name, result);
   }
   catch (OverflowException)
   {
      Console.WriteLine("The {0} value {1} is outside the range of the Byte type.",
                        number.GetType().Name, number);
   }
}
// The example displays the following output:
//       Converted the UInt64 value 0 to the Byte value 0.
//       Converted the UInt64 value 121 to the Byte value 121.
//       The UInt64 value 340 is outside the range of the Byte type.
//       The UInt64 value 18446744073709551615 is outside the range of the Byte type.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToByte(SByte)

Kaynak:
Convert.cs
Kaynak:
Convert.cs
Kaynak:
Convert.cs

Önemli

Bu API, CLS uyumlu değildir.

Belirtilen 8 bit işaretli tamsayı değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

C#
[System.CLSCompliant(false)]
public static byte ToByte (sbyte value);

Parametreler

value
SByte

Dönüştürülecek 8 bit işaretli tamsayı.

Döndürülenler

ile eşdeğer valuebir 8 bit işaretsiz tamsayı.

Öznitelikler

Özel durumlar

Örnekler

Aşağıdaki örnek bir değer dizisini SByte değerlere Byte dönüştürür.

C#
sbyte[] numbers = { SByte.MinValue, -1, 0, 10, SByte.MaxValue };
byte result;
foreach (sbyte number in numbers)
{
   try {
      result = Convert.ToByte(number);
      Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                        number.GetType().Name, number,
                        result.GetType().Name, result);
   }
   catch (OverflowException)
   {
      Console.WriteLine("The {0} value {1} is outside the range of the Byte type.",
                        number.GetType().Name, number);
   }
}
// The example displays the following output:
//       The SByte value -128 is outside the range of the Byte type.
//       The SByte value -1 is outside the range of the Byte type.
//       Converted the SByte value 0 to the Byte value 0.
//       Converted the SByte value 10 to the Byte value 10.
//       Converted the SByte value 127 to the Byte value 127.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToByte(DateTime)

Kaynak:
Convert.cs
Kaynak:
Convert.cs
Kaynak:
Convert.cs

Bu yöntemin çağrılması her zaman oluşturur InvalidCastException.

C#
public static byte ToByte (DateTime value);

Parametreler

value
DateTime

Dönüştürülecek tarih ve saat değeri.

Döndürülenler

Bu dönüşüm desteklenmiyor. Hiçbir değer döndürülmez.

Özel durumlar

Bu dönüşüm desteklenmiyor.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ToByte(Int64)

Kaynak:
Convert.cs
Kaynak:
Convert.cs
Kaynak:
Convert.cs

Belirtilen 64 bit işaretli tamsayı değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

C#
public static byte ToByte (long value);

Parametreler

value
Int64

Dönüştürülecek 64 bit işaretli tamsayı.

Döndürülenler

ile eşdeğer valuebir 8 bit işaretsiz tamsayı.

Özel durumlar

value , Byte.MinValue değerinden küçük veya Byte.MaxValue değerinden büyük.

Örnekler

Aşağıdaki örnek bir değer dizisini Int64 değerlere Byte dönüştürür.

C#
long[] numbers = { Int64.MinValue, -1, 0, 121, 340, Int64.MaxValue };
byte result;
foreach (long number in numbers)
{
   try {
      result = Convert.ToByte(number);
      Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                        number.GetType().Name, number,
                        result.GetType().Name, result);
   }
   catch (OverflowException) {
      Console.WriteLine("The {0} value {1} is outside the range of the Byte type.",
                        number.GetType().Name, number);
   }
}
// The example displays the following output:
//       The Int64 value -9223372036854775808 is outside the range of the Byte type.
//       The Int64 value -1 is outside the range of the Byte type.
//       Converted the Int64 value 0 to the Byte value 0.
//       Converted the Int64 value 121 to the Byte value 121.
//       The Int64 value 340 is outside the range of the Byte type.
//       The Int64 value 9223372036854775807 is outside the range of the Byte type.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToByte(Byte)

Kaynak:
Convert.cs
Kaynak:
Convert.cs
Kaynak:
Convert.cs

Belirtilen 8 bit işaretsiz tamsayıyı döndürür; gerçek dönüştürme gerçekleştirilmemesi.

C#
public static byte ToByte (byte value);

Parametreler

value
Byte

Döndürülecek 8 bit işaretsiz tamsayı.

Döndürülenler

value değiştirilmeden döndürülür.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToByte(Char)

Kaynak:
Convert.cs
Kaynak:
Convert.cs
Kaynak:
Convert.cs

Belirtilen Unicode karakterinin değerini eşdeğer 8 bit işaretsiz tamsayıya dönüştürür.

C#
public static byte ToByte (char value);

Parametreler

value
Char

Dönüştürülecek Unicode karakteri.

Döndürülenler

ile eşdeğer valuebir 8 bit işaretsiz tamsayı.

Özel durumlar

value Byte.MaxValue değerinden büyük bir sayıyı temsil eder.

Örnekler

Aşağıdaki örnek bir değer dizisini Char değerlere Byte dönüştürür.

C#
char[] chars = { 'a', 'z', '\x0007', '\x03FF' };
foreach (char ch in chars)
{
   try {
      byte result = Convert.ToByte(ch);
      Console.WriteLine("{0} is converted to {1}.", ch, result);
   }
   catch (OverflowException) {
      Console.WriteLine("Unable to convert u+{0} to a byte.",
                        Convert.ToInt16(ch).ToString("X4"));
   }
}
// The example displays the following output:
//       a is converted to 97.
//       z is converted to 122.
//        is converted to 7.
//       Unable to convert u+03FF to a byte.

Açıklamalar

Bu yöntem, ona geçirilen nesnenin sayısal kodunu Char temsil eden imzasız bir bayt değeri döndürür. .NET'te nesne Char 16 bit değerdir. Bu, yöntemin ASCII karakter aralığında veya Unicode C0 Denetimleri ile Temel Latin ve C1 Denetimleri ile Latin-1 Ek aralıklarında, U+0000'den U+00FF'ye kadar karakterlerin sayısal kodlarını döndürmek için uygun olduğu anlamına gelir.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToByte(Decimal)

Kaynak:
Convert.cs
Kaynak:
Convert.cs
Kaynak:
Convert.cs

Belirtilen ondalık sayının değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

C#
public static byte ToByte (decimal value);

Parametreler

value
Decimal

Dönüştürülecek sayı.

Döndürülenler

value, en yakın 8 bit işaretsiz tamsayıya yuvarlanmış. İki tamsayının ortasındaysa value çift sayı döndürülür; yani 4,5 4'e, 5,5 ise 6'ya dönüştürülür.

Özel durumlar

value , Byte.MaxValue değerinden büyük veya Byte.MinValue değerinden küçüktür.

Açıklamalar

Aşağıdaki örnek bir Byte değeri değerine Decimal ve Decimal değerini Bytedeğerine dönüştürür.

C#
public void ConvertByteDecimal(byte byteVal) {
    decimal decimalVal;

    // Byte to decimal conversion will not overflow.
    decimalVal = System.Convert.ToDecimal(byteVal);
    System.Console.WriteLine("The byte as a decimal is {0}.",
        decimalVal);

    // Decimal to byte conversion can overflow.
    try {
        byteVal = System.Convert.ToByte(decimalVal);
        System.Console.WriteLine("The Decimal as a byte is {0}.",
            byteVal);
    }
    catch (System.OverflowException) {
        System.Console.WriteLine(
            "The decimal value is too large for a byte.");
    }
}

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToByte(Boolean)

Kaynak:
Convert.cs
Kaynak:
Convert.cs
Kaynak:
Convert.cs

Belirtilen Boole değerini eşdeğer 8 bit işaretsiz tamsayıya dönüştürür.

C#
public static byte ToByte (bool value);

Parametreler

value
Boolean

Dönüştürülecek Boole değeri.

Döndürülenler

1 value ise, değilse 0 sayısıdır true.

Örnekler

Aşağıdaki örnekte, değerinin değerlere dönüştürülmesi BooleanByte gösterilmektedir.

C#
bool falseFlag = false;
bool trueFlag = true;

Console.WriteLine("{0} converts to {1}.", falseFlag,
                  Convert.ToByte(falseFlag));
Console.WriteLine("{0} converts to {1}.", trueFlag,
                  Convert.ToByte(trueFlag));
// The example displays the following output:
//       False converts to 0.
//       True converts to 1.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToByte(Int16)

Kaynak:
Convert.cs
Kaynak:
Convert.cs
Kaynak:
Convert.cs

Belirtilen 16 bit işaretli tamsayı değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

C#
public static byte ToByte (short value);

Parametreler

value
Int16

Dönüştürülecek 16 bitlik işaretli tamsayı.

Döndürülenler

ile eşdeğer valuebir 8 bit işaretsiz tamsayı.

Özel durumlar

value , Byte.MinValue değerinden küçük veya Byte.MaxValue değerinden büyük.

Örnekler

Aşağıdaki örnek bir değer dizisini Int16 değerlere Byte dönüştürür.

C#
short[] numbers = { Int16.MinValue, -1, 0, 121, 340, Int16.MaxValue };
byte result;
foreach (short number in numbers)
{
   try {
      result = Convert.ToByte(number);
      Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                        number.GetType().Name, number,
                        result.GetType().Name, result);
   }
   catch (OverflowException) {
      Console.WriteLine("The {0} value {1} is outside the range of the Byte type.",
                        number.GetType().Name, number);
   }
}
// The example displays the following output:
//       The Int16 value -32768 is outside the range of the Byte type.
//       The Int16 value -1 is outside the range of the Byte type.
//       Converted the Int16 value 0 to the Byte value 0.
//       Converted the Int16 value 121 to the Byte value 121.
//       The Int16 value 340 is outside the range of the Byte type.
//       The Int16 value 32767 is outside the range of the Byte type.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToByte(Int32)

Kaynak:
Convert.cs
Kaynak:
Convert.cs
Kaynak:
Convert.cs

Belirtilen 32 bit işaretli tamsayı değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

C#
public static byte ToByte (int value);

Parametreler

value
Int32

Dönüştürülecek 32 bit işaretli tamsayı.

Döndürülenler

ile eşdeğer valuebir 8 bit işaretsiz tamsayı.

Özel durumlar

value , Byte.MinValue değerinden küçük veya Byte.MaxValue değerinden büyük.

Örnekler

Aşağıdaki örnek bir değer dizisini Int32 değerlere Byte dönüştürür.

C#
int[] numbers = { Int32.MinValue, -1, 0, 121, 340, Int32.MaxValue };
byte result;
foreach (int number in numbers)
{
   try {
      result = Convert.ToByte(number);
      Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                        number.GetType().Name, number,
                        result.GetType().Name, result);
   }
   catch (OverflowException) {
      Console.WriteLine("The {0} value {1} is outside the range of the Byte type.",
                        number.GetType().Name, number);
   }
}
// The example displays the following output:
//       The Int32 value -2147483648 is outside the range of the Byte type.
//       The Int32 value -1 is outside the range of the Byte type.
//       Converted the Int32 value 0 to the Byte value 0.
//       Converted the Int32 value 121 to the Byte value 121.
//       The Int32 value 340 is outside the range of the Byte type.
//       The Int32 value 2147483647 is outside the range of the Byte type.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToByte(Double)

Kaynak:
Convert.cs
Kaynak:
Convert.cs
Kaynak:
Convert.cs

Belirtilen çift duyarlıklı kayan noktalı sayının değerini eşdeğer bir 8 bit işaretsiz tamsayıya dönüştürür.

C#
public static byte ToByte (double value);

Parametreler

value
Double

Dönüştürülecek çift duyarlıklı kayan nokta sayısı.

Döndürülenler

value, en yakın 8 bit işaretsiz tamsayıya yuvarlanmış. İki tamsayının ortasındaysa value çift sayı döndürülür; yani 4,5 4'e, 5,5 ise 6'ya dönüştürülür.

Özel durumlar

value , Byte.MaxValue değerinden büyük veya Byte.MinValue değerinden küçüktür.

Örnekler

Aşağıdaki örnek bir Byte değeri değerine Double ve Double değerini Bytedeğerine dönüştürür.

C#
public void ConvertDoubleByte(double doubleVal) {
    byte	byteVal = 0;

    // Double to byte conversion can overflow.
    try {
        byteVal = System.Convert.ToByte(doubleVal);
        System.Console.WriteLine("{0} as a byte is: {1}.",
            doubleVal, byteVal);
    }
    catch (System.OverflowException) {
        System.Console.WriteLine(
            "Overflow in double-to-byte conversion.");
    }

    // Byte to double conversion cannot overflow.
    doubleVal = System.Convert.ToDouble(byteVal);
    System.Console.WriteLine("{0} as a double is: {1}.",
        byteVal, doubleVal);
}

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToByte(Object)

Kaynak:
Convert.cs
Kaynak:
Convert.cs
Kaynak:
Convert.cs

Belirtilen nesnenin değerini 8 bit işaretsiz tamsayıya dönüştürür.

C#
public static byte ToByte (object value);
C#
public static byte ToByte (object? value);

Parametreler

value
Object

arabirimini IConvertible uygulayan bir nesne veya null.

Döndürülenler

ile eşdeğer valuebir 8 bit işaretsiz tamsayı veya ise value sıfır.null

Özel durumlar

value bir değerin özellik biçiminde Byte değil.

value uygulamaz IConvertible.

-veya-

türünden value türüne Byte dönüştürme desteklenmez.

value Byte.MinValue değerinden küçük veya Byte.MaxValue değerinden büyük bir sayıyı temsil eder.

Örnekler

Aşağıdaki örnek, bir nesne dizisini ToByte(Object) değerlere dönüştürmek için Byte yöntemini kullanır.

C#
object[] values = { true, -12, 163, 935, 'x', "104", "103.0", "-1",
                    "1.00e2", "One", 1.00e2};
byte result;
foreach (object value in values)
{
   try {
      result = Convert.ToByte(value);
      Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                        value.GetType().Name, value,
                        result.GetType().Name, result);
   }
   catch (OverflowException)
   {
      Console.WriteLine("The {0} value {1} is outside the range of the Byte type.",
                        value.GetType().Name, value);
   }
   catch (FormatException)
   {
      Console.WriteLine("The {0} value {1} is not in a recognizable format.",
                        value.GetType().Name, value);
   }
   catch (InvalidCastException)
   {
      Console.WriteLine("No conversion to a Byte exists for the {0} value {1}.",
                        value.GetType().Name, value);
   }
}
// The example displays the following output:
//       Converted the Boolean value True to the Byte value 1.
//       The Int32 value -12 is outside the range of the Byte type.
//       Converted the Int32 value 163 to the Byte value 163.
//       The Int32 value 935 is outside the range of the Byte type.
//       Converted the Char value x to the Byte value 120.
//       Converted the String value 104 to the Byte value 104.
//       The String value 103.0 is not in a recognizable format.
//       The String value -1 is outside the range of the Byte type.
//       The String value 1.00e2 is not in a recognizable format.
//       The String value One is not in a recognizable format.
//       Converted the Double value 100 to the Byte value 100.

Açıklamalar

değilse valuenull, bu yöntem temel alınan türünün uygulamasına IConvertible.ToByte bir çağrı sarmalar value.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0