C# BitConverter.ToUInt64 方法


C# 中的 BitConverter.ToUInt64() 方法用於返回在位元組陣列指定位置由八個位元組轉換來的 64 位無符號整數。

語法

public static ulong ToUInt64 (byte[] val, int begnIndex);

示例

以上內容中,val 為位元組陣列,而 begnIndex 是 val 中的起始位置。

現場演示

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 - 7; i = i + 8) {          ulong res = BitConverter.ToUInt64(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 = 1083970061066240256

示例

現場演示

using System;
public class Demo {
   public static void Main() {
      byte[] arr = { 0, 0, 1, 3, 5, 7, 9, 11, 15, 0, 1, 6, 8, 10, 20, 25, 36, 34 };
      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 - 7; i = i + 8) {          ulong res = BitConverter.ToUInt64(arr, i);          Console.WriteLine("
Value = "+arr[i]);          Console.WriteLine("Result = "+res);       }    } }

輸出

Byte Array...
0
0
1
3
5
7
9
11
15
0
1
6
8
10
20
25
36
34
Byte Array (String representation) = 00-00-01-03-05-07-09-0B-0F-00-01-06-08-0A-14-19-24-22
Value = 0
Result = 1083970061066240256
Value = 0
Result = 2601132293100011776

更新日期: 2019 年 12 月 2 日

217 次瀏覽

開啟你的 職業 生涯

透過完成課程取得認證

立即開始
廣告
© . All rights reserved.