Byte 構造体
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
8 ビット符号なし整数を表します。
public value class System::Byte : IComparable, IComparable<System::Byte>, IConvertible, IEquatable<System::Byte>, IFormattable
public value class System::Byte : IComparable<System::Byte>, IConvertible, IEquatable<System::Byte>, IParsable<System::Byte>, ISpanParsable<System::Byte>, IUtf8SpanParsable<System::Byte>, System::Numerics::IAdditionOperators<System::Byte, System::Byte, System::Byte>, System::Numerics::IAdditiveIdentity<System::Byte, System::Byte>, System::Numerics::IBinaryInteger<System::Byte>, System::Numerics::IBinaryNumber<System::Byte>, System::Numerics::IBitwiseOperators<System::Byte, System::Byte, System::Byte>, System::Numerics::IComparisonOperators<System::Byte, System::Byte, bool>, System::Numerics::IDecrementOperators<System::Byte>, System::Numerics::IDivisionOperators<System::Byte, System::Byte, System::Byte>, System::Numerics::IEqualityOperators<System::Byte, System::Byte, bool>, System::Numerics::IIncrementOperators<System::Byte>, System::Numerics::IMinMaxValue<System::Byte>, System::Numerics::IModulusOperators<System::Byte, System::Byte, System::Byte>, System::Numerics::IMultiplicativeIdentity<System::Byte, System::Byte>, System::Numerics::IMultiplyOperators<System::Byte, System::Byte, System::Byte>, System::Numerics::INumber<System::Byte>, System::Numerics::INumberBase<System::Byte>, System::Numerics::IShiftOperators<System::Byte, int, System::Byte>, System::Numerics::ISubtractionOperators<System::Byte, System::Byte, System::Byte>, System::Numerics::IUnaryNegationOperators<System::Byte, System::Byte>, System::Numerics::IUnaryPlusOperators<System::Byte, System::Byte>, System::Numerics::IUnsignedNumber<System::Byte>
public value class System::Byte : IComparable, IComparable<System::Byte>, IConvertible, IEquatable<System::Byte>, ISpanFormattable
public value class System::Byte : IComparable<System::Byte>, IConvertible, IEquatable<System::Byte>, IParsable<System::Byte>, ISpanParsable<System::Byte>, System::Numerics::IAdditionOperators<System::Byte, System::Byte, System::Byte>, System::Numerics::IAdditiveIdentity<System::Byte, System::Byte>, System::Numerics::IBinaryInteger<System::Byte>, System::Numerics::IBinaryNumber<System::Byte>, System::Numerics::IBitwiseOperators<System::Byte, System::Byte, System::Byte>, System::Numerics::IComparisonOperators<System::Byte, System::Byte, bool>, System::Numerics::IDecrementOperators<System::Byte>, System::Numerics::IDivisionOperators<System::Byte, System::Byte, System::Byte>, System::Numerics::IEqualityOperators<System::Byte, System::Byte, bool>, System::Numerics::IIncrementOperators<System::Byte>, System::Numerics::IMinMaxValue<System::Byte>, System::Numerics::IModulusOperators<System::Byte, System::Byte, System::Byte>, System::Numerics::IMultiplicativeIdentity<System::Byte, System::Byte>, System::Numerics::IMultiplyOperators<System::Byte, System::Byte, System::Byte>, System::Numerics::INumber<System::Byte>, System::Numerics::INumberBase<System::Byte>, System::Numerics::IShiftOperators<System::Byte, int, System::Byte>, System::Numerics::ISubtractionOperators<System::Byte, System::Byte, System::Byte>, System::Numerics::IUnaryNegationOperators<System::Byte, System::Byte>, System::Numerics::IUnaryPlusOperators<System::Byte, System::Byte>, System::Numerics::IUnsignedNumber<System::Byte>
public value class System::Byte : IComparable, IConvertible, IFormattable
public value class System::Byte : IComparable, IComparable<System::Byte>, IEquatable<System::Byte>, IFormattable
public struct Byte : IComparable, IComparable<byte>, IConvertible, IEquatable<byte>, IFormattable
public readonly struct Byte : IComparable<byte>, IConvertible, IEquatable<byte>, IParsable<byte>, ISpanParsable<byte>, IUtf8SpanParsable<byte>, System.Numerics.IAdditionOperators<byte,byte,byte>, System.Numerics.IAdditiveIdentity<byte,byte>, System.Numerics.IBinaryInteger<byte>, System.Numerics.IBinaryNumber<byte>, System.Numerics.IBitwiseOperators<byte,byte,byte>, System.Numerics.IComparisonOperators<byte,byte,bool>, System.Numerics.IDecrementOperators<byte>, System.Numerics.IDivisionOperators<byte,byte,byte>, System.Numerics.IEqualityOperators<byte,byte,bool>, System.Numerics.IIncrementOperators<byte>, System.Numerics.IMinMaxValue<byte>, System.Numerics.IModulusOperators<byte,byte,byte>, System.Numerics.IMultiplicativeIdentity<byte,byte>, System.Numerics.IMultiplyOperators<byte,byte,byte>, System.Numerics.INumber<byte>, System.Numerics.INumberBase<byte>, System.Numerics.IShiftOperators<byte,int,byte>, System.Numerics.ISubtractionOperators<byte,byte,byte>, System.Numerics.IUnaryNegationOperators<byte,byte>, System.Numerics.IUnaryPlusOperators<byte,byte>, System.Numerics.IUnsignedNumber<byte>
public readonly struct Byte : IComparable, IComparable<byte>, IConvertible, IEquatable<byte>, IFormattable
public readonly struct Byte : IComparable, IComparable<byte>, IConvertible, IEquatable<byte>, ISpanFormattable
public readonly struct Byte : IComparable<byte>, IConvertible, IEquatable<byte>, IParsable<byte>, ISpanParsable<byte>, System.Numerics.IAdditionOperators<byte,byte,byte>, System.Numerics.IAdditiveIdentity<byte,byte>, System.Numerics.IBinaryInteger<byte>, System.Numerics.IBinaryNumber<byte>, System.Numerics.IBitwiseOperators<byte,byte,byte>, System.Numerics.IComparisonOperators<byte,byte,bool>, System.Numerics.IDecrementOperators<byte>, System.Numerics.IDivisionOperators<byte,byte,byte>, System.Numerics.IEqualityOperators<byte,byte,bool>, System.Numerics.IIncrementOperators<byte>, System.Numerics.IMinMaxValue<byte>, System.Numerics.IModulusOperators<byte,byte,byte>, System.Numerics.IMultiplicativeIdentity<byte,byte>, System.Numerics.IMultiplyOperators<byte,byte,byte>, System.Numerics.INumber<byte>, System.Numerics.INumberBase<byte>, System.Numerics.IShiftOperators<byte,int,byte>, System.Numerics.ISubtractionOperators<byte,byte,byte>, System.Numerics.IUnaryNegationOperators<byte,byte>, System.Numerics.IUnaryPlusOperators<byte,byte>, System.Numerics.IUnsignedNumber<byte>
[System.Serializable]
public struct Byte : IComparable, IConvertible, IFormattable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct Byte : IComparable, IComparable<byte>, IConvertible, IEquatable<byte>, IFormattable
public struct Byte : IComparable, IComparable<byte>, IEquatable<byte>, IFormattable
type byte = struct
interface IConvertible
interface IFormattable
type byte = struct
interface IConvertible
interface IFormattable
interface IParsable<byte>
interface ISpanFormattable
interface ISpanParsable<byte>
interface IUtf8SpanFormattable
interface IUtf8SpanParsable<byte>
interface IAdditionOperators<byte, byte, byte>
interface IAdditiveIdentity<byte, byte>
interface IBinaryInteger<byte>
interface IBinaryNumber<byte>
interface IBitwiseOperators<byte, byte, byte>
interface IComparisonOperators<byte, byte, bool>
interface IEqualityOperators<byte, byte, bool>
interface IDecrementOperators<byte>
interface IDivisionOperators<byte, byte, byte>
interface IIncrementOperators<byte>
interface IModulusOperators<byte, byte, byte>
interface IMultiplicativeIdentity<byte, byte>
interface IMultiplyOperators<byte, byte, byte>
interface INumber<byte>
interface INumberBase<byte>
interface ISubtractionOperators<byte, byte, byte>
interface IUnaryNegationOperators<byte, byte>
interface IUnaryPlusOperators<byte, byte>
interface IShiftOperators<byte, int, byte>
interface IMinMaxValue<byte>
interface IUnsignedNumber<byte>
type byte = struct
interface IConvertible
interface ISpanFormattable
interface IFormattable
type byte = struct
interface IConvertible
interface IFormattable
interface IParsable<byte>
interface ISpanFormattable
interface ISpanParsable<byte>
interface IAdditionOperators<byte, byte, byte>
interface IAdditiveIdentity<byte, byte>
interface IBinaryInteger<byte>
interface IBinaryNumber<byte>
interface IBitwiseOperators<byte, byte, byte>
interface IComparisonOperators<byte, byte, bool>
interface IEqualityOperators<byte, byte, bool>
interface IDecrementOperators<byte>
interface IDivisionOperators<byte, byte, byte>
interface IIncrementOperators<byte>
interface IModulusOperators<byte, byte, byte>
interface IMultiplicativeIdentity<byte, byte>
interface IMultiplyOperators<byte, byte, byte>
interface INumber<byte>
interface INumberBase<byte>
interface ISubtractionOperators<byte, byte, byte>
interface IUnaryNegationOperators<byte, byte>
interface IUnaryPlusOperators<byte, byte>
interface IShiftOperators<byte, int, byte>
interface IMinMaxValue<byte>
interface IUnsignedNumber<byte>
type byte = struct
interface IConvertible
interface IFormattable
interface IParsable<byte>
interface ISpanFormattable
interface ISpanParsable<byte>
interface IAdditionOperators<byte, byte, byte>
interface IAdditiveIdentity<byte, byte>
interface IBinaryInteger<byte>
interface IBinaryNumber<byte>
interface IBitwiseOperators<byte, byte, byte>
interface IComparisonOperators<byte, byte, bool>
interface IEqualityOperators<byte, byte, bool>
interface IDecrementOperators<byte>
interface IDivisionOperators<byte, byte, byte>
interface IIncrementOperators<byte>
interface IModulusOperators<byte, byte, byte>
interface IMultiplicativeIdentity<byte, byte>
interface IMultiplyOperators<byte, byte, byte>
interface INumber<byte>
interface INumberBase<byte>
interface ISubtractionOperators<byte, byte, byte>
interface IUnaryNegationOperators<byte, byte>
interface IUnaryPlusOperators<byte, byte>
interface IUtf8SpanFormattable
interface IUtf8SpanParsable<byte>
interface IShiftOperators<byte, int, byte>
interface IMinMaxValue<byte>
interface IUnsignedNumber<byte>
[<System.Serializable>]
type byte = struct
interface IFormattable
interface IConvertible
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type byte = struct
interface IFormattable
interface IConvertible
type byte = struct
interface IFormattable
Public Structure Byte
Implements IComparable, IComparable(Of Byte), IConvertible, IEquatable(Of Byte), IFormattable
Public Structure Byte
Implements IAdditionOperators(Of Byte, Byte, Byte), IAdditiveIdentity(Of Byte, Byte), IBinaryInteger(Of Byte), IBinaryNumber(Of Byte), IBitwiseOperators(Of Byte, Byte, Byte), IComparable(Of Byte), IComparisonOperators(Of Byte, Byte, Boolean), IConvertible, IDecrementOperators(Of Byte), IDivisionOperators(Of Byte, Byte, Byte), IEqualityOperators(Of Byte, Byte, Boolean), IEquatable(Of Byte), IIncrementOperators(Of Byte), IMinMaxValue(Of Byte), IModulusOperators(Of Byte, Byte, Byte), IMultiplicativeIdentity(Of Byte, Byte), IMultiplyOperators(Of Byte, Byte, Byte), INumber(Of Byte), INumberBase(Of Byte), IParsable(Of Byte), IShiftOperators(Of Byte, Integer, Byte), ISpanParsable(Of Byte), ISubtractionOperators(Of Byte, Byte, Byte), IUnaryNegationOperators(Of Byte, Byte), IUnaryPlusOperators(Of Byte, Byte), IUnsignedNumber(Of Byte), IUtf8SpanParsable(Of Byte)
Public Structure Byte
Implements IComparable, IComparable(Of Byte), IConvertible, IEquatable(Of Byte), ISpanFormattable
Public Structure Byte
Implements IAdditionOperators(Of Byte, Byte, Byte), IAdditiveIdentity(Of Byte, Byte), IBinaryInteger(Of Byte), IBinaryNumber(Of Byte), IBitwiseOperators(Of Byte, Byte, Byte), IComparable(Of Byte), IComparisonOperators(Of Byte, Byte, Boolean), IConvertible, IDecrementOperators(Of Byte), IDivisionOperators(Of Byte, Byte, Byte), IEqualityOperators(Of Byte, Byte, Boolean), IEquatable(Of Byte), IIncrementOperators(Of Byte), IMinMaxValue(Of Byte), IModulusOperators(Of Byte, Byte, Byte), IMultiplicativeIdentity(Of Byte, Byte), IMultiplyOperators(Of Byte, Byte, Byte), INumber(Of Byte), INumberBase(Of Byte), IParsable(Of Byte), IShiftOperators(Of Byte, Integer, Byte), ISpanParsable(Of Byte), ISubtractionOperators(Of Byte, Byte, Byte), IUnaryNegationOperators(Of Byte, Byte), IUnaryPlusOperators(Of Byte, Byte), IUnsignedNumber(Of Byte)
Public Structure Byte
Implements IComparable, IConvertible, IFormattable
Public Structure Byte
Implements IComparable, IComparable(Of Byte), IEquatable(Of Byte), IFormattable
- 継承
- 属性
- 実装
-
IComparable IComparable<Byte> IConvertible IEquatable<Byte> IFormattable IComparable<TSelf> IEquatable<TSelf> IParsable<Byte> IParsable<TSelf> ISpanFormattable ISpanParsable<Byte> ISpanParsable<TSelf> IUtf8SpanFormattable IUtf8SpanParsable<Byte> IUtf8SpanParsable<TSelf> IAdditionOperators<Byte,Byte,Byte> IAdditionOperators<TSelf,TSelf,TSelf> IAdditiveIdentity<Byte,Byte> IAdditiveIdentity<TSelf,TSelf> IBinaryInteger<Byte> IBinaryNumber<Byte> IBinaryNumber<TSelf> IBitwiseOperators<Byte,Byte,Byte> IBitwiseOperators<TSelf,TSelf,TSelf> IComparisonOperators<Byte,Byte,Boolean> IComparisonOperators<TSelf,TSelf,Boolean> IDecrementOperators<Byte> IDecrementOperators<TSelf> IDivisionOperators<Byte,Byte,Byte> IDivisionOperators<TSelf,TSelf,TSelf> IEqualityOperators<Byte,Byte,Boolean> IEqualityOperators<TSelf,TOther,TResult> IEqualityOperators<TSelf,TSelf,Boolean> IIncrementOperators<Byte> IIncrementOperators<TSelf> IMinMaxValue<Byte> IModulusOperators<Byte,Byte,Byte> IModulusOperators<TSelf,TSelf,TSelf> IMultiplicativeIdentity<Byte,Byte> IMultiplicativeIdentity<TSelf,TSelf> IMultiplyOperators<Byte,Byte,Byte> IMultiplyOperators<TSelf,TSelf,TSelf> INumber<Byte> INumber<TSelf> INumberBase<Byte> INumberBase<TSelf> IShiftOperators<Byte,Int32,Byte> IShiftOperators<TSelf,Int32,TSelf> ISubtractionOperators<Byte,Byte,Byte> ISubtractionOperators<TSelf,TSelf,TSelf> IUnaryNegationOperators<Byte,Byte> IUnaryNegationOperators<TSelf,TSelf> IUnaryPlusOperators<Byte,Byte> IUnaryPlusOperators<TSelf,TSelf> IUnsignedNumber<Byte>
注釈
Byte は、0 ( Byte.MinValue 定数で表される) から 255 ( Byte.MaxValue 定数で表される) までの範囲の値を持つ符号なし整数を表す不変の値型です。 .NET には、-128 から 127 までの範囲の値を表す符号付き 8 ビット整数値型 ( SByte) も含まれています。
Byte 値をインスタンス化する
Byte値は、いくつかの方法でインスタンス化できます。
Byte変数を宣言し、Byteデータ型の範囲内にあるリテラル整数値を割り当てることができます。 次の例では、2 つの Byte 変数を宣言し、この方法で値を割り当てます。
byte value1 = 64; byte value2 = 255;let value1 = 64uy let value2 = 255uyDim value1 As Byte = 64 Dim value2 As Byte = 255バイト以外の数値をバイトに割り当てることができます。 これは縮小変換であるため、C# と F# のキャスト演算子、または visual Basic の変換メソッド (
Option Strictがオンの場合) が必要です。 バイト以外の値が小数部を含む Single、 Double、または Decimal 値である場合、その小数部の処理は、変換を実行するコンパイラによって異なります。 次の例では、複数の数値を Byte 変数に割り当てます。int int1 = 128; try { byte value1 = (byte)int1; Console.WriteLine(value1); } catch (OverflowException) { Console.WriteLine($"{int1} is out of range of a byte."); } double dbl2 = 3.997; try { byte value2 = (byte)dbl2; Console.WriteLine(value2); } catch (OverflowException) { Console.WriteLine($"{dbl2} is out of range of a byte."); } // The example displays the following output: // 128 // 3let int1 = 128 try let value1 = byte int1 printfn $"{value1}" with :? OverflowException -> printfn $"{int1} is out of range of a byte." let dbl2 = 3.997 try let value2 = byte dbl2 printfn $"{value2}" with :? OverflowException -> printfn $"{dbl2} is out of range of a byte." // The example displays the following output: // 128 // 3Dim int1 As Integer = 128 Try Dim value1 As Byte = CByte(int1) Console.WriteLine(value1) Catch e As OverflowException Console.WriteLine("{0} is out of range of a byte.", int1) End Try Dim dbl2 As Double = 3.997 Try Dim value2 As Byte = CByte(dbl2) Console.WriteLine(value2) Catch e As OverflowException Console.WriteLine("{0} is out of range of a byte.", dbl2) End Try ' The example displays the following output: ' 128 ' 4Convert クラスのメソッドを呼び出して、サポートされている任意の型をByte値に変換できます。 これは、 Byte が IConvertible インターフェイスをサポートしているためです。 次の例は、 Int32 値の配列から Byte 値への変換を示しています。
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.let numbers = [| Int32.MinValue; -1; 0; 121; 340; Int32.MaxValue |] for number in numbers do try let result = Convert.ToByte number printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}." with :? OverflowException -> printfn $"The {number.GetType().Name} value {number} is outside the range of the Byte type." // 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.Dim numbers() As Integer = {Int32.MinValue, -1, 0, 121, 340, Int32.MaxValue} Dim result As Byte For Each number As Integer 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 e As OverflowException Console.WriteLine("The {0} value {1} is outside the range of the Byte type.", number.GetType().Name, number) End Try Next ' 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.ParseまたはTryParseメソッドを呼び出して、Byte値の文字列形式をByteに変換できます。 文字列には、10 進数または 16 進数を含めることができます。 次の例は、10 進文字列と 16 進文字列の両方を使用した解析操作を示しています。
string string1 = "244"; try { byte byte1 = Byte.Parse(string1); Console.WriteLine(byte1); } catch (OverflowException) { Console.WriteLine($"'{string1}' is out of range of a byte."); } catch (FormatException) { Console.WriteLine($"'{string1}' is out of range of a byte."); } string string2 = "F9"; try { byte byte2 = Byte.Parse(string2, System.Globalization.NumberStyles.HexNumber); Console.WriteLine(byte2); } catch (OverflowException) { Console.WriteLine($"'{string2}' is out of range of a byte."); } catch (FormatException) { Console.WriteLine($"'{string2}' is out of range of a byte."); } // The example displays the following output: // 244 // 249let string1 = "244" try let byte1 = Byte.Parse string1 printfn $"{byte1}" with | :? OverflowException -> printfn $"'{string1}' is out of range of a byte." | :? FormatException -> printfn $"'{string1}' is out of range of a byte." let string2 = "F9" try let byte2 = Byte.Parse(string2, System.Globalization.NumberStyles.HexNumber) printfn $"{byte2}" with | :? OverflowException -> printfn $"'{string2}' is out of range of a byte." | :? FormatException -> printfn $"'{string2}' is out of range of a byte." // The example displays the following output: // 244 // 249Dim string1 As String = "244" Try Dim byte1 As Byte = Byte.Parse(string1) Console.WriteLine(byte1) Catch e As OverflowException Console.WriteLine("'{0}' is out of range of a byte.", string1) Catch e As FormatException Console.WriteLine("'{0}' is out of range of a byte.", string1) End Try Dim string2 As String = "F9" Try Dim byte2 As Byte = Byte.Parse(string2, System.Globalization.NumberStyles.HexNumber) Console.WriteLine(byte2) Catch e As OverflowException Console.WriteLine("'{0}' is out of range of a byte.", string2) Catch e As FormatException Console.WriteLine("'{0}' is out of range of a byte.", string2) End Try ' The example displays the following output: ' 244 ' 249
バイト値に対して操作を実行する
Byte型は、加算、減算、除算、乗算、減算、否定、単項否定などの標準的な数学演算をサポートします。 他の整数型と同様に、 Byte 型もビットごとの AND、 OR、 XOR、左シフト、および右シフト演算子をサポートします。
標準の数値演算子を使用して 2 つの Byte 値を比較したり、 CompareTo または Equals メソッドを呼び出したりできます。
また、 Math クラスのメンバーを呼び出して、数値の絶対値の取得、整数除算からの商と剰余の計算、2 つの整数の最大値または最小値の決定、数値の符号の取得、数値の丸めなど、さまざまな数値演算を実行することもできます。
バイトを文字列として表す
Byte型は、標準およびカスタムの数値書式指定文字列を完全にサポートします。 (詳細については、「 書式設定の種類、 標準の数値書式指定文字列、 およびカスタム数値書式指定文字列」を参照してください)。ただし、最も一般的に、バイト値は 1 桁から 3 桁の値で表され、書式設定は追加されず、2 桁の 16 進数の値として表されます。
Byte値を、先頭にゼロがない整数文字列として書式設定するには、パラメーターなしのToString() メソッドを呼び出します。 "D" 書式指定子を使用すると、文字列形式に指定した数の先行ゼロを含めることもできます。 "X" 書式指定子を使用すると、 Byte 値を 16 進数の文字列として表すことができます。 次の例では、これらの 3 つの方法で Byte 値の配列内の要素を書式設定します。
byte[] numbers = [ 0, 16, 104, 213 ];
foreach (byte number in numbers)
{
// Display value using default formatting.
Console.Write("{0,-3} --> ", number.ToString());
// Display value with 3 digits and leading zeros.
Console.Write(number.ToString("D3") + " ");
// Display value with hexadecimal.
Console.Write(number.ToString("X2") + " ");
// Display value with four hexadecimal digits.
Console.WriteLine(number.ToString("X4"));
}
// The example displays the following output:
// 0 --> 000 00 0000
// 16 --> 016 10 0010
// 104 --> 104 68 0068
// 213 --> 213 D5 00D5
let numbers = [| 0; 16; 104; 213 |]
for number in numbers do
// Display value using default formatting.
number.ToString()
|> printf "%-3s --> "
// Display value with 3 digits and leading zeros.
number.ToString "D3"
|> printf "%s "
// Display value with hexadecimal.
number.ToString "X2"
|> printf "%s "
// Display value with four hexadecimal digits.
number.ToString "X4"
|> printfn "%s"
// The example displays the following output:
// 0 --> 000 00 0000
// 16 --> 016 10 0010
// 104 --> 104 68 0068
// 213 --> 213 D5 00D5
Dim numbers() As Byte = {0, 16, 104, 213}
For Each number As Byte In numbers
' Display value using default formatting.
Console.Write("{0,-3} --> ", number.ToString())
' Display value with 3 digits and leading zeros.
Console.Write(number.ToString("D3") + " ")
' Display value with hexadecimal.
Console.Write(number.ToString("X2") + " ")
' Display value with four hexadecimal digits.
Console.WriteLine(number.ToString("X4"))
Next
' The example displays the following output:
' 0 --> 000 00 0000
' 16 --> 016 10 0010
' 104 --> 104 68 0068
' 213 --> 213 D5 00D5
Byte メソッドを呼び出し、メソッドの 2 番目のパラメーターとして base を指定することで、ToString(Byte, Int32)値をバイナリ、8 進数、10 進数、または 16 進数の文字列として書式設定することもできます。 次の例では、このメソッドを呼び出して、バイト値の配列のバイナリ、8 進数、および 16 進数の表現を表示します。
byte[] numbers = { 0, 16, 104, 213 };
Console.WriteLine("{0} {1,8} {2,5} {3,5}",
"Value", "Binary", "Octal", "Hex");
foreach (byte number in numbers)
{
Console.WriteLine("{0,5} {1,8} {2,5} {3,5}",
number, Convert.ToString(number, 2),
Convert.ToString(number, 8),
Convert.ToString(number, 16));
}
// The example displays the following output:
// Value Binary Octal Hex
// 0 0 0 0
// 16 10000 20 10
// 104 1101000 150 68
// 213 11010101 325 d5
let numbers = [| 0; 16; 104; 213 |]
printfn "%s %8s %5s %5s" "Value" "Binary" "Octal" "Hex"
for number in numbers do
printfn $"%5i{number} %8s{Convert.ToString(number, 2)} %5s{Convert.ToString(number, 8)} %5s{Convert.ToString(number, 16)}"
// The example displays the following output:
// Value Binary Octal Hex
// 0 0 0 0
// 16 10000 20 10
// 104 1101000 150 68
// 213 11010101 325 d5
Dim numbers() As Byte = {0, 16, 104, 213}
Console.WriteLine("{0} {1,8} {2,5} {3,5}",
"Value", "Binary", "Octal", "Hex")
For Each number As Byte In numbers
Console.WriteLine("{0,5} {1,8} {2,5} {3,5}",
number, Convert.ToString(number, 2),
Convert.ToString(number, 8),
Convert.ToString(number, 16))
Next
' The example displays the following output:
' Value Binary Octal Hex
' 0 0 0 0
' 16 10000 20 10
' 104 1101000 150 68
' 213 11010101 325 d5
10 進数以外のバイト値を操作する
個々のバイトを 10 進値として処理するだけでなく、バイト値を使用してビットごとの演算を実行したり、バイト配列を操作したり、バイト値のバイナリ表現または 16 進数表現を使用したりできます。 たとえば、 BitConverter.GetBytes メソッドのオーバーロードは、各プリミティブ データ型をバイト配列に変換でき、 BigInteger.ToByteArray メソッドは BigInteger 値をバイト配列に変換します。
Byte 値は、符号ビットなしで、その大きさでのみ 8 ビットで表されます。 これは、 Byte 値に対してビットごとの操作を実行する場合や、個々のビットを操作する場合に注意することが重要です。 2 つの 10 進数以外の値に対して数値、ブール値、または比較演算を実行するには、両方の値で同じ表現を使用する必要があります。
2 つの Byte 値に対して操作を実行すると、値は同じ表現を共有するため、結果は正確です。 これは次の例で示されています。この例では、 Byte 値の最下位ビットをマスクして、偶数になるようにします。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] values = [ Convert.ToString(12, 16),
Convert.ToString(123, 16),
Convert.ToString(245, 16) ];
byte mask = 0xFE;
foreach (string value in values) {
Byte byteValue = Byte.Parse(value, NumberStyles.AllowHexSpecifier);
Console.WriteLine($"{byteValue} And {mask} = {byteValue & mask}");
}
}
}
// The example displays the following output:
// 12 And 254 = 12
// 123 And 254 = 122
// 245 And 254 = 244
open System
open System.Globalization
let values =
[ Convert.ToString(12, 16)
Convert.ToString(123, 16)
Convert.ToString(245, 16) ]
let mask = 0xFEuy
for value in values do
let byteValue = Byte.Parse(value, NumberStyles.AllowHexSpecifier)
printfn $"{byteValue} And {mask} = {byteValue &&& mask}"
// The example displays the following output:
// 12 And 254 = 12
// 123 And 254 = 122
// 245 And 254 = 244
Imports System.Globalization
Module Example1
Public Sub Main()
Dim values() As String = {Convert.ToString(12, 16),
Convert.ToString(123, 16),
Convert.ToString(245, 16)}
Dim mask As Byte = &HFE
For Each value As String In values
Dim byteValue As Byte = Byte.Parse(value, NumberStyles.AllowHexSpecifier)
Console.WriteLine("{0} And {1} = {2}", byteValue, mask,
byteValue And mask)
Next
End Sub
End Module
' The example displays the following output:
' 12 And 254 = 12
' 123 And 254 = 122
' 245 And 254 = 244
一方、符号なしビットと符号付きビットの両方を使用する場合、ビットごとの演算は、 SByte 値が正の値に符号と大きさの表現を使用し、負の値に対して 2 の補数表現が使用されるため、複雑になります。 意味のあるビットごとの演算を実行するには、値を 2 つの同等の表現に変換する必要があり、符号ビットに関する情報を保持する必要があります。 次の例では、8 ビット符号付き値と符号なし値の配列のビット 2 と 4 をマスクします。
using System;
using System.Collections.Generic;
using System.Globalization;
public struct ByteString
{
public string Value;
public int Sign;
}
public class BSExample1
{
public static void Main()
{
ByteString[] values = CreateArray(-15, 123, 245);
byte mask = 0x14; // Mask all bits but 2 and 4.
foreach (ByteString strValue in values)
{
byte byteValue = Byte.Parse(strValue.Value, NumberStyles.AllowHexSpecifier);
Console.WriteLine($"{strValue.Sign * byteValue} ({Convert.ToString(byteValue, 2)}) And {mask} ({Convert.ToString(mask, 2)}) = {(strValue.Sign & Math.Sign(mask)) * (byteValue & mask)} ({Convert.ToString(byteValue & mask, 2)})");
}
}
private static ByteString[] CreateArray(params int[] values)
{
List<ByteString> byteStrings = new();
foreach (object value in values)
{
ByteString temp = new ByteString();
int sign = Math.Sign((int)value);
temp.Sign = sign;
// Change two's complement to magnitude-only representation.
temp.Value = Convert.ToString(((int)value) * sign, 16);
byteStrings.Add(temp);
}
return byteStrings.ToArray();
}
}
// The example displays the following output:
// -15 (1111) And 20 (10100) = 4 (100)
// 123 (1111011) And 20 (10100) = 16 (10000)
// 245 (11110101) And 20 (10100) = 20 (10100)
open System
open System.Collections.Generic
open System.Globalization
[<Struct>]
type ByteString =
{ Sign: int
Value: string }
let createArray values =
[ for value in values do
let sign = sign value
{ Sign = sign
// Change two's complement to magnitude-only representation.
Value = Convert.ToString(value * sign, 16)} ]
let values = createArray [ -15; 123; 245 ]
let mask = 0x14uy // Mask all bits but 2 and 4.
for strValue in values do
let byteValue = Byte.Parse(strValue.Value, NumberStyles.AllowHexSpecifier)
printfn $"{strValue.Sign * int byteValue} ({Convert.ToString(byteValue, 2)}) And {mask} ({Convert.ToString(mask, 2)}) = {(strValue.Sign &&& (int mask |> sign)) * int (byteValue &&& mask)} ({Convert.ToString(byteValue &&& mask, 2)})"
// The example displays the following output:
// -15 (1111) And 20 (10100) = 4 (100)
// 123 (1111011) And 20 (10100) = 16 (10000)
// 245 (11110101) And 20 (10100) = 20 (10100)
Imports System.Collections.Generic
Imports System.Globalization
Public Structure ByteString
Public Value As String
Public Sign As Integer
End Structure
Module Example2
Public Sub Main()
Dim values() As ByteString = CreateArray(-15, 123, 245)
Dim mask As Byte = &H14 ' Mask all bits but 2 and 4.
For Each strValue As ByteString In values
Dim byteValue As Byte = Byte.Parse(strValue.Value, NumberStyles.AllowHexSpecifier)
Console.WriteLine("{0} ({1}) And {2} ({3}) = {4} ({5})",
strValue.Sign * byteValue,
Convert.ToString(byteValue, 2),
mask, Convert.ToString(mask, 2),
(strValue.Sign And Math.Sign(mask)) * (byteValue And mask),
Convert.ToString(byteValue And mask, 2))
Next
End Sub
Private Function CreateArray(ParamArray values() As Object) As ByteString()
Dim byteStrings As New List(Of ByteString)
For Each value As Object In values
Dim temp As New ByteString()
Dim sign As Integer = Math.Sign(value)
temp.Sign = sign
' Change two's complement to magnitude-only representation.
value = value * sign
temp.Value = Convert.ToString(value, 16)
byteStrings.Add(temp)
Next
Return byteStrings.ToArray()
End Function
End Module
' The example displays the following output:
' -15 (1111) And 20 (10100) = 4 (100)
' 123 (1111011) And 20 (10100) = 16 (10000)
' 245 (11110101) And 20 (10100) = 20 (10100)
フィールド
| 名前 | 説明 |
|---|---|
| MaxValue |
Byteの可能な最大値を表します。 このフィールドは定数です。 |
| MinValue |
Byteの可能な限り小さい値を表します。 このフィールドは定数です。 |
メソッド
| 名前 | 説明 |
|---|---|
| Clamp(Byte, Byte, Byte) |
値を包括的な最小値と最大値にクランプします。 |
| CompareTo(Byte) |
このインスタンスを指定した 8 ビット符号なし整数と比較し、その相対値を示す値を返します。 |
| CompareTo(Object) |
このインスタンスを指定したオブジェクトと比較し、その相対値を示す値を返します。 |
| CreateChecked<TOther>(TOther) |
現在の型のインスタンスを値から作成し、現在の型の表現可能範囲外の値に対してオーバーフロー例外をスローします。 |
| CreateSaturating<TOther>(TOther) |
現在の型のインスタンスを値から作成し、現在の型の表現可能な範囲外の値を飽和させます。 |
| CreateTruncating<TOther>(TOther) |
現在の型のインスタンスを値から作成し、現在の型の表現可能範囲外の値を切り捨てます。 |
| DivRem(Byte, Byte) |
2 つの値の商と剰余を計算します。 |
| Equals(Byte) |
このインスタンスと指定した Byte オブジェクトが同じ値を表すかどうかを示す値を返します。 |
| Equals(Object) |
このインスタンスが指定したオブジェクトと等しいかどうかを示す値を返します。 |
| GetHashCode() |
このインスタンスのハッシュ コードを返します。 |
| GetTypeCode() | |
| IsEvenInteger(Byte) |
値が偶数の整数を表すかどうかを判断します。 |
| IsOddInteger(Byte) |
値が奇数の整数を表すかどうかを判断します。 |
| IsPow2(Byte) |
値が 2 の累乗であるかどうかを判断します。 |
| LeadingZeroCount(Byte) |
値の先頭のゼロの数を計算します。 |
| Log10(Byte) |
8 ビット符号なし整数を表します。 |
| Log2(Byte) |
値の log2 を計算します。 |
| Max(Byte, Byte) |
2 つの値を、より大きいコンピューティングと比較します。 |
| Min(Byte, Byte) |
2 つの値を比較して計算します。計算の方が少なくなります。 |
| Parse(ReadOnlySpan<Byte>, IFormatProvider) |
UTF-8 文字のスパンを値に解析します。 |
| Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider) |
UTF-8 文字のスパンを値に解析します。 |
| Parse(ReadOnlySpan<Char>, IFormatProvider) |
文字のスパンを値に解析します。 |
| Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
指定したスタイルおよびカルチャ固有の形式の数値のスパン表現を、同等の Byte に変換します。 |
| Parse(String, IFormatProvider) |
指定したカルチャ固有の形式の数値の文字列形式を、同等の Byte に変換します。 |
| Parse(String, NumberStyles, IFormatProvider) |
指定したスタイルおよびカルチャ固有の形式の数値の文字列形式を、同等の Byte に変換します。 |
| Parse(String, NumberStyles) |
指定したスタイルの数値の文字列形式を等価の Byte に変換します。 |
| Parse(String) |
数値の文字列形式を等価の Byte に変換します。 |
| PopCount(Byte) |
値に設定されているビット数を計算します。 |
| RotateLeft(Byte, Int32) |
指定した量の値を左に回転します。 |
| RotateRight(Byte, Int32) |
指定した量の値を右に回転します。 |
| Sign(Byte) |
値の符号を計算します。 |
| ToString() |
現在の Byte オブジェクトの値を等価の文字列形式に変換します。 |
| ToString(IFormatProvider) |
指定したカルチャ固有の書式設定情報を使用して、現在の Byte オブジェクトの数値を等価の文字列形式に変換します。 |
| ToString(String, IFormatProvider) |
現在の Byte オブジェクトの値を、指定した書式およびカルチャ固有の書式設定情報を使用して、同等の文字列形式に変換します。 |
| ToString(String) |
指定した形式を使用して、現在の Byte オブジェクトの値を等価の文字列形式に変換します。 |
| TrailingZeroCount(Byte) |
値の末尾のゼロの数を計算します。 |
| TryFormat(Span<Byte>, Int32, ReadOnlySpan<Char>, IFormatProvider) |
現在のインスタンスの値を UTF-8 として指定されたバイトスパンに書式設定しようとします。 |
| TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider) |
現在の 8 ビット符号なし整数インスタンスの値を、指定された文字スパンに書式設定しようとします。 |
| TryParse(ReadOnlySpan<Byte>, Byte) |
数値の文字列形式を含む UTF-8 文字スパンを、等価の 8 ビット符号なし整数に変換しようとします。 |
| TryParse(ReadOnlySpan<Byte>, IFormatProvider, Byte) |
UTF-8 文字のスパンを値に解析しようとします。 |
| TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Byte) |
UTF-8 文字のスパンを値に解析しようとします。 |
| TryParse(ReadOnlySpan<Char>, Byte) |
数値のスパン表現を等価の Byte に変換し、変換が成功したかどうかを示す値を返します。 |
| TryParse(ReadOnlySpan<Char>, IFormatProvider, Byte) |
文字のスパンを値に解析しようとします。 |
| TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Byte) |
指定したスタイルおよびカルチャ固有の形式の数値のスパン表現を、同等の Byte に変換します。 戻り値は、変換が成功したか失敗したかを示します。 |
| TryParse(String, Byte) |
数値の文字列形式を等価の Byte に変換し、変換が成功したかどうかを示す値を返します。 |
| TryParse(String, IFormatProvider, Byte) |
文字列を値に解析しようとします。 |
| TryParse(String, NumberStyles, IFormatProvider, Byte) |
指定したスタイルおよびカルチャ固有の形式の数値の文字列形式を、同等の Byte に変換します。 戻り値は、変換が成功したか失敗したかを示します。 |
明示的なインターフェイスの実装
適用対象
スレッド セーフ
この型のすべてのメンバーはスレッド セーフです。 インスタンスの状態を変更するように見えるメンバーは、実際には新しい値で初期化された新しいインスタンスを返します。 他の型と同様に、この型のインスタンスを含む共有変数の読み取りと書き込みは、スレッド セーフを保証するためにロックによって保護する必要があります。