• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# Byte结构体代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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);
}
开发者ID:.NET开发者,项目名称:System,代码行数:17,代码来源:Byte

输出:

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);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:15,代码来源:Byte

输出:

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
开发者ID:.NET开发者,项目名称:System,代码行数:27,代码来源:Byte


示例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"));
}
开发者ID:.NET开发者,项目名称:System,代码行数:11,代码来源:Byte

输出:

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));
}
开发者ID:.NET开发者,项目名称:System,代码行数:9,代码来源:Byte

输出:

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);
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:20,代码来源:Byte

输出:

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();
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:47,代码来源:Byte

输出:

-15 (1111) And 20 (10100) = 4 (100)
123 (1111011) And 20 (10100) = 16 (10000)
245 (11110101) And 20 (10100) = 20 (10100)



注:本文中的System.Byte结构体示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# Char结构体代码示例发布时间:2022-05-26
下一篇:
C# Boolean结构体代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap