我在第一步(i=0)时出错了“i=0”。这个代码有什么问题?
Dim byteArray As Byte() = { _
0, 54, 101, 196, 255, 255, 255, 255, 0, 0, _
0, 0, 0, 0, 0, 0, 128, 0, 202, 154, _
59, 0, 0, 0, 0, 1, 0, 0, 0, 0, _
255, 255, 255, 255, 1, 0, 0, 255, 255, 255, _
255, 255, 255, 255, 127, 86, 85, 85, 85, 85, _
85, 255, 255, 170, 170, 170, 170, 170, 170, 0, _
0, 100, 167, 179, 182, 224, 13, 0, 0, 156, _
88, 76, 73, 31, 242}
Dim UintList As New List(Of UInt64)
For i As Integer = 0 To byteArray.Count - 1 Step 8
UintList.Add(BitConverter.ToInt64(byteArray, i))
Next发布于 2016-11-19 13:19:29
代码中有两个错误。
BitConverter将您的字节转换为Int64值,尝试将其插入到UInt64集合中。这可能导致OverflowException,,因为UInt64不能表示负值。
您需要将BitConverter生成的内容类型与列表存储的内容匹配起来,因此,请执行以下任何一项操作(两者都不是两者兼得!):- Replace `BitConverter.ToInt64(…)` with `BitConverter.ToUInt64(…)`.
- Declare `Dim UintList As New List(Of Int64)` instead of as `List(Of UInt64)`.
ArgumentException。BitConverter.ToInt64期望从指定的起始偏移量i中至少有8个字节可用。但是,一旦它偏移了72,只剩下4个字节,这不足以产生一个Int64。
因此,您需要检查是否有足够的字节来进行转换:
对于I作为整数=0到byteArray.Count -1步骤8,如果我+8 <= byteArray.Length然后…“有足够的字节可转换为64位整数否则…‘剩下的字节不足以转换为64位整数结束下一步https://stackoverflow.com/questions/40692244
复制相似问题