2012-11-24 86 views
0

這是一段提供例外的Visual Basic代碼,FormatExceptionVisual Basic文件中的格式異常

假設我的十六進制值變量hexValue是424E0A78。代碼正常工作。當下hexValue得到的值(比方說)424EA78,出現此異常:

提供的十六進制值是空或者格式不正確。使用以下格式:00000000

是否有解決方案?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Try 
     Dim hexValue As String = "424E0A78" 

     Dim iInputIndex As Integer = 0 
     Dim iOutputIndex As Integer = 0 
     Dim bArray(3) As Byte 'creating an array 
     Dim rnum As Integer 
     Dim rArray(3) As Byte 
     Dim rSingle As Single 
     Dim rSingle1 As Single 
     Dim rSingle2 As Single 
     Dim rStr As Integer 
     Dim rnewArray(3) As Byte ' variables 
     Dim rnum2, rnum3 As Integer 'variables 
     Dim rstr2, rstr3 As String 

     'For iInputIndex = 0 To hexValue.Length - 1 Step 2 
     For iInputIndex = 0 To hexValue.Length - 1 Step 2 

      'rnum3 = hexValue.Chars(iInputIndex) * 16 

      'REjina code starts 
      rStr = 0 
      rstr2 = hexValue.Chars(iInputIndex) 
      If (rstr2 = "A") Then 
       rstr2 = 10 
      ElseIf (rstr2 = "B") Then 
       rstr2 = 11 
      ElseIf (rstr2 = "C") Then 
       rstr2 = 12 
      ElseIf (rstr2 = "D") Then 
       rstr2 = 13 
      ElseIf (rstr2 = "E") Then 
       rstr2 = 14 
      ElseIf (rstr2 = "F") Then 
       rstr2 = 15 
      End If 

      rStr = Val(rstr2) * 16 

      'Second rejina conversion 
      rstr2 = hexValue.Chars(iInputIndex + 1) 
      If (rstr2 = "A") Then 
       rstr2 = 10 
      ElseIf (rstr2 = "B") Then 
       rstr2 = 11 
      ElseIf (rstr2 = "C") Then 
       rstr2 = 12 
      ElseIf (rstr2 = "D") Then 
       rstr2 = 13 
      ElseIf (rstr2 = "E") Then 
       rstr2 = 14 
      ElseIf (rstr2 = "F") Then 
       rstr2 = 15 
      End If 

      rStr = rStr + Val(rstr2) 

      'rstr2 = hexValue.Chars(iInputIndex + 1) 
      rnewArray(iOutputIndex) = rStr 
      'rejina code ends 

      'rArray(iOutputIndex) = Byte.Parse(hexValue.Chars(iInputIndex) & hexValue.Chars(iInputIndex + 1), Globalization.NumberStyles.HexNumber) 

      'bArray(iOutputIndex) = Convert.ToByte(hexValue.Chars(iInputIndex)) 

      iOutputIndex += 1 
     Next 

     'Array.Reverse(rArray) 

     'rejina code starts 
     Array.Reverse(rnewArray) 
     'rejina code ends 

     'Array.Reverse(bArray) 


     'rSingle = BitConverter.ToSingle(rArray, 0) 
     'rejina code starts 
     rSingle1 = BitConverter.ToSingle(rnewArray, 0) 
     'rejina code ends 
     'rSingle2 = BitConverter.ToSingle(bArray, 0) 

     MessageBox.Show(rSingle1) 

     'Return BitConverter.ToSingle(rnewArray, 0) 
    Catch ex As Exception 
     Throw New FormatException("The supplied hex value is either empty or in an incorrect format. Use the following format: 00000000", ex) 
    End Try 
End Sub 
+5

Puhleeze不寫這樣的代碼。使用Integer.TryParse()和NumberStyles.HexNumber參數。 –

+0

我以前只用過Parse。因爲它也拋出了同樣的例外,我就這樣去了。但是在調試了很多之後,這個代碼也出現了相同的異常。所以我只想知道VB是否提供了任何方法來克服這種異常。謝謝。 – user46646

+0

你得到的異常類型是什麼?你的catch塊捕獲任何異常,所以很難知道確切的異常。但@HansPassant是正確的,你可以通過使用TryParse(而不是Parse)大大減少這段代碼。 –

回答

1

如果輸入字符串有一個奇怪的(即不連)長,那麼你會得到一個數組索引越界異常就行了:

hexValue.Chars(iInputIndex + 1) 

...這是最有可能你在這裏遇到什麼。

+0

是的。你能告訴我VB是否提供了任何方法來克服這種異常? – user46646

+1

提示:== 123(換句話說:如果你的輸入是奇數長度,只需加上一個前導零)。並按照Hans Passants的建議 - 使用TryParse/Parse! – igrimpe