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

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

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

下面是msdn中的一個(gè)例子,在我剛看到這里例子時(shí),該例子有三點(diǎn)是我可以學(xué)到的。

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


 如果,定義所占的位置少于要輸入的字符,會(huì)自動(dòng)增加,而不是截?cái)唷?/p>

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

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


 

 

下面是這個(gè)例子的完整代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BitConverter數(shù)據(jù)轉(zhuǎn)換
{
  class Program
  {
    //排列格式,第一個(gè)變量占五個(gè)位置,第二個(gè)變量占17個(gè)位置,第三個(gè)變量占10個(gè)位置
    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用于基礎(chǔ)數(shù)據(jù)跟字節(jié)數(shù)組相互轉(zhuǎn)換
      //BitConverter.ToUInt16()方法將字節(jié)數(shù)組指定位置起的兩個(gè)字節(jié)轉(zhuǎn)換為無符號(hào)整數(shù)
      ushort value = BitConverter.ToUInt16(bytes, index);
      //BitConverter.ToString()字節(jié)數(shù)組轉(zhuǎn)換為十六進(jì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();
    }
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論