日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > C# >内容正文

C#

java中bitconverter_C#中BitConverter.ToUInt16()和BitConverter.ToString()的简单使用

發(fā)布時(shí)間:2025/4/5 C# 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中bitconverter_C#中BitConverter.ToUInt16()和BitConverter.ToString()的简单使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

下面是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í)有所幫助,也希望大家多多支持腳本之家。

總結(jié)

以上是生活随笔為你收集整理的java中bitconverter_C#中BitConverter.ToUInt16()和BitConverter.ToString()的简单使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。