网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C#中BitConverter.ToUInt16()和BitConverter.ToString()怎么用

这篇文章主要为大家展示了“C#中BitConverter.ToUInt16()和BitConverter.ToString()怎么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“C#中BitConverter.ToUInt16()和BitConverter.ToString()怎么用”这篇文章吧。

大同网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、自适应网站建设等网站项目制作,到程序开发,运营维护。创新互联成立于2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联

第一:排列格式。如:定义一个常量变量const string a="{0,11}{1,10},{2,7}"; 这样一个格式用来排列三个变量的位置,第一个变量占5个位置,第二个变量占8个位置,第三个变量占10个位置。中英文都算一个位置。比如在控制台上输出 Console.WriteLine(a,"以后想找什么当另外一半","找个又帅又有车的","那买副象棋吧");下面是这个测试的截图

如果,定义所占的位置少于要输入的字符,会自动增加,而不是截断。

第二:BitConverter.ToUInt16()的用法,是把两个字节转换为无符号整数,如:205 56 这两个字节的16进制是 CD 38 那么转为无符号整数 应该倒过来排 即 38CD 这个数转为无符号十进制整数就是 14541

第三:BitConverter.ToString()的用法,这个就是把字节或字节数组转换为十六进制或十六进制的字符串形式,中间用“-”连接

下面是这个例子的完整代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace BitConverter数据转换{  class Program  {    //排列格式,第一个变量占五个位置,第二个变量占17个位置,第三个变量占10个位置    const string formatter = "{0,5}{1,17}{2,10}";     // Convert two byte array elements to a ushort and display it.    public static void BAToUInt16(byte[] bytes, int index)    {      //BitConverter用于基础数据跟字节数组相互转换      //BitConverter.ToUInt16()方法将字节数组指定位置起的两个字节转换为无符号整数      ushort value = BitConverter.ToUInt16(bytes, index);      //BitConverter.ToString()字节数组转换为十六进制的字符串形式      Console.WriteLine(formatter, index,        BitConverter.ToString(bytes, index, 2), value);    }    static void Main(string[] args)    {      byte[] byteArray = {      15, 0, 0, 255, 3, 16, 39, 255, 255, 127 };      Console.WriteLine(        "This example of the BitConverter.ToUInt16( byte[ ], " +        "int ) \nmethod generates the following output. It " +        "converts elements \nof a byte array to ushort values.\n");      Console.WriteLine("initial byte array");      Console.WriteLine("------------------");      Console.WriteLine(BitConverter.ToString(byteArray));      Console.WriteLine();      Console.WriteLine(formatter, "index", "array elements",        "ushort");      Console.WriteLine(formatter, "-----", "--------------",        "------");      // Convert byte array elements to ushort values.      BAToUInt16(byteArray, 1);      BAToUInt16(byteArray, 0);      BAToUInt16(byteArray, 3);      BAToUInt16(byteArray, 5);      BAToUInt16(byteArray, 8);      BAToUInt16(byteArray, 7);      Console.ReadKey();    }  }}

以上是“C#中BitConverter.ToUInt16()和BitConverter.ToString()怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


网页题目:C#中BitConverter.ToUInt16()和BitConverter.ToString()怎么用
分享地址:http://bjjierui.cn/article/pehidh.html

其他资讯