C# BitConverter.ToUInt32() 方法


C# 中的 BitConverter.ToUInt32() 方法用於從位元組陣列指定位置的四個位元組返回一個 32 位無符號整數。

語法

public static uint ToUInt32 (byte[] val, int begnIndex);

上面,val 是位元組陣列,而 begnIndex 是 val 中的起始位置。

示例

 即時演示

using System;
public class Demo {
   public static void Main() {
      byte[] arr = { 0, 3, 5, 10, 15, 2};
      int count = arr.Length;
      Console.Write("Byte Array... ");
      for (int i = 0; i < count; i++) {
         Console.Write("
"+arr[i]);       }       Console.WriteLine("

Byte Array (String representation) = {0} ",       BitConverter.ToString(arr));       for (int i = 1; i < arr.Length - 1; i = i + 4) {          uint res = BitConverter.ToUInt32(arr, i);          Console.WriteLine("
Value = "+arr[i]);          Console.WriteLine("Result = "+res);       }    } }

輸出

Byte Array...
0
3
5
10
15
2
Byte Array (String representation) = 00-03-05-0A-0F-02
Value = 3
Result = 252314883

示例

 即時演示

using System;
public class Demo {
   public static void Main() {
      byte[] arr = { 0, 0, 1, 3, 5, 7, 9, 11, 15 };
      int count = arr.Length;
      Console.Write("Byte Array... ");
      for (int i = 0; i < count; i++) {
         Console.Write("
"+arr[i]);       }       Console.WriteLine("

Byte Array (String representation) = {0} ",       BitConverter.ToString(arr));       for (int i = 1; i < arr.Length - 1; i = i + 4) {          uint res = BitConverter.ToUInt32(arr, i);          Console.WriteLine("
Value = "+arr[i]);          Console.WriteLine("Result = "+res);       }    } }

輸出

Byte Array...
0
0
1
3
5
7
9
11
15
Byte Array (String representation) = 00-00-01-03-05-07-09-0B-0F
Value = 0
Result = 84082944
Value = 7
Result = 252381447

更新於: 02-12-2019

235 瀏覽

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.