本文整理匯總了C#中System.Byte結構體的典型用法代碼示例。如果您正苦於以下問題:C# Byte結構體的具體用法?C# Byte怎麽用?C# Byte使用的例子?那麽, 這裏精選的結構體代碼示例或許可以為您提供幫助。
Byte結構體屬於System命名空間,在下文中一共展示了Byte結構體的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1:
int int1 = 128;
try {
byte value1 = (byte) int1;
Console.WriteLine(value1);
}
catch (OverflowException) {
Console.WriteLine("{0} is out of range of a byte.", int1);
}
double dbl2 = 3.997;
try {
byte value2 = (byte) dbl2;
Console.WriteLine(value2);
}
catch (OverflowException) {
Console.WriteLine("{0} is out of range of a byte.", dbl2);
}
輸出:
128 3
示例2: foreach
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 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.
示例3: catch
string string1 = "244";
try {
byte byte1 = Byte.Parse(string1);
Console.WriteLine(byte1);
}
catch (OverflowException) {
Console.WriteLine("'{0}' is out of range of a byte.", string1);
}
catch (FormatException) {
Console.WriteLine("'{0}' is out of range of a byte.", string1);
}
string string2 = "F9";
try {
byte byte2 = Byte.Parse(string2,
System.Globalization.NumberStyles.HexNumber);
Console.WriteLine(byte2);
}
catch (OverflowException) {
Console.WriteLine("'{0}' is out of range of a byte.", string2);
}
catch (FormatException) {
Console.WriteLine("'{0}' is out of range of a byte.", string2);
}
// The example displays the following output:
// 244
// 249
示例4:
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"));
}
輸出:
0 --> 000 00 0000 16 --> 016 10 0010 104 --> 104 68 0068 213 --> 213 D5 00D5
示例5:
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));
}
輸出:
Value Binary Octal Hex 0 0 0 0 16 10000 20 10 104 1101000 150 68 213 11010101 325 d5
示例6: Main
//引入命名空間
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("{0} And {1} = {2}", byteValue, mask,
byteValue & mask);
}
}
}
輸出:
12 And 254 = 12 123 And 254 = 122 245 And 254 = 244
示例7: Main
//引入命名空間
using System;
using System.Collections.Generic;
using System.Globalization;
public struct ByteString
{
public string Value;
public int Sign;
}
public class Example
{
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("{0} ({1}) And {2} ({3}) = {4} ({5})",
strValue.Sign * byteValue,
Convert.ToString(byteValue, 2),
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 List<ByteString>();
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();
}
}
輸出:
-15 (1111) And 20 (10100) = 4 (100) 123 (1111011) And 20 (10100) = 16 (10000) 245 (11110101) And 20 (10100) = 20 (10100)