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("\n"+arr[i]); } Console.WriteLine("\n\n字节数组(字符串表示形式) = {0} ", BitConverter.ToString(arr)); for (int i = 1; i < arr.Length - 1; i = i + 4) { uint res = BitConverter.ToUInt32(arr, i); Console.WriteLine("\nValue = "+arr[i]); Console.WriteLine("Result = "+res); } } }
输出结果
Byte Array... 0 3 5 10 15 2 字节数组(字符串表示形式) = 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("\n"+arr[i]); } Console.WriteLine("\n\n字节数组(字符串表示形式) = {0} ", BitConverter.ToString(arr)); for (int i = 1; i < arr.Length - 1; i = i + 4) { uint res = BitConverter.ToUInt32(arr, i); Console.WriteLine("\nValue = "+arr[i]); Console.WriteLine("Result = "+res); } } }
输出结果
Byte Array... 0 0 1 3 5 7 9 11 15 字节数组(字符串表示形式) = 00-00-01-03-05-07-09-0B-0F Value = 0 Result = 84082944 Value = 7 Result = 252381447