2016-11-19 5 views
0

最初のステップ(i = 0)でエラー "OverflowException"が発生しました。このコードで何が間違っていますか?BitConverter.ToInt64 OverflowException

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 
+0

は、あなたはそれが 'ArgumentException'ないのですか? – dasblinkenlight

+1

http://stackoverflow.com/questions/9804265/how-does-bitconverter-toint64-handles-a-byte-arry-with-32-bytes-a-256-bit-hash –

+1

Put Option Strict On the topコンパイラがなぜこれが間違っているのかを知るのを助けるソースコードファイルの。この例外をスローするBitConverterではありません。これは、無効なInt64からUInt64への変換です。代わりにBitConverter.ToUInt64()を使用してください。または、List(Of Int64)が必要な場合は、推測が困難です。 –

答えて

1

コードに2つのエラーがあります。

  1. あなたがUInt64コレクションに挿入しようとInt64値のために、バイトを変換BitConverterてみましょう。 UInt64は負の値を表すことができないため、OverflowExceptionが発生する可能性があります。あなたはBitConverterは何あなたのリストを格納して製造するものの種類を一致させるため、次のいずれかの実行する必要があり

    (両方の両方ではないが!):

    • BitConverter.ToUInt64(…)BitConverter.ToInt64(…)を交換してください。
    • List(Of UInt64)の代わりにDim UintList As New List(Of Int64)を宣言してください。
  2. あなたの配列は、最後のループ反復でArgumentExceptionを引き起こすであろう、8で割り切れない長(75バイト)を有します。 BitConverter.ToInt64は、指定された開始オフセットiから少なくとも8バイトが使用可能であると予測します。ただし、72がオフセットされると、残っているのはわずか4バイトで、Int64を生成するには不十分です。

    したがって、あなたは十分なバイトがあるかどうかを確認する必要がありますが変換するには、左:

    For i As Integer = 0 To byteArray.Count - 1 Step 8 
        If i + 8 <= byteArray.Length Then 
         … ' enough bytes available to convert to a 64-bit integer 
        Else 
         … ' not enough bytes left to convert to a 64-bit integer 
        End 
    Next 
    
+0

多くの感謝.. ToInt64 ToUInt64を変更すると今働いて.. –

関連する問題