欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中BitConverter.ToUInt16()和BitConverter.ToString()的簡單使用

 更新時間:2020年02月01日 14:59:45   作者:育良書記  
這篇文章主要介紹了C#中BitConverter.ToUInt16()和BitConverter.ToString()的簡單使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

下面是msdn中的一個例子,在我剛看到這里例子時,該例子有三點是我可以學到的。

第一:排列格式。如:定義一個常量變量const  string  a="{0,11}{1,10},{2,7}"; 這樣一個格式用來排列三個變量的位置,第一個變量占5個位置,第二個變量占8個位置,第三個變量占10個位置。中英文都算一個位置。比如在控制臺上輸出 Console.WriteLine(a,"以后想找什么當另外一半","找個又帥又有車的","那買副象棋吧"); 下面是這個測試的截圖


 如果,定義所占的位置少于要輸入的字符,會自動增加,而不是截斷。

第二:BitConverter.ToUInt16()的用法,是把兩個字節(jié)轉(zhuǎn)換為無符號整數(shù),如:205 56  這兩個字節(jié)的16進制是 CD 38  那么轉(zhuǎn)為無符號整數(shù) 應該倒過來排 即 38CD  這個數(shù)轉(zhuǎn)為無符號十進制整數(shù)就是 14541

第三:BitConverter.ToString()的用法,這個就是把字節(jié)或字節(jié)數(shù)組轉(zhuǎn)換為十六進制或十六進制的字符串形式,中間用“-”連接


 

 

下面是這個例子的完整代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BitConverter數(shù)據(jù)轉(zhuǎn)換
{
  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用于基礎數(shù)據(jù)跟字節(jié)數(shù)組相互轉(zhuǎn)換
      //BitConverter.ToUInt16()方法將字節(jié)數(shù)組指定位置起的兩個字節(jié)轉(zhuǎn)換為無符號整數(shù)
      ushort value = BitConverter.ToUInt16(bytes, index);
      //BitConverter.ToString()字節(jié)數(shù)組轉(zhuǎn)換為十六進制的字符串形式
      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();
    }
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論