2016-06-09 42 views
-1

行:如何解決異常當字符串是「9f」時,輸入字符串的格式不正確?

currentFrameInt = System.Convert.ToInt32(currentFramestr); 

在這種情況下,我在currentFramestr看到值「9F」 包括字符串的開頭有三個空格。

的currentFramestr是更新progressBar1的一部分:

if (strFFOUT.Contains("frame=")) 
{ 
    currentFramestr = strFFOUT.Substring(7, 6); 
    currentFramestr = currentFramestr.Trim(); 
    currentFrameInt = System.Convert.ToInt32(currentFramestr, 16); 
} 


string percentage = System.Convert.ToInt32((ProgressBar1.Value/ProgressBar1.Maximum * 100)).ToString() + "%"; 
ProgressBar1.Maximum = FCount + 1000; 
ProgressBar1.Value = (currentFrameInt); 

什麼我想要做的是更新進度條值。

也許整個方法代碼會給我更多的線索,我想要什麼和使用currentFramestr。一般來說,我想更新progressBar1值。

private void Convert() 
{ 
    Control.CheckForIllegalCrossThreadCalls = false; 

    if (ComboBox1.SelectedIndex == 3) 
    { 
     strFFCMD = " -i \"" + InputFile + "\" \"" + OutputFile + "\""; 
    } 

    if (ComboBox1.SelectedIndex == 2) 
    { 
     strFFCMD = " -i " + (char)34 + InputFile + (char)34 + 
     " -c:v libx264 -s 1280x720 -pix_fmt yuv420p -qp 20 -profile high444-c:a libvo_aacenc -b:a 128k -ar 44100 -ac 2 " + OutputFile; 
    } 

    psiProcInfo.FileName = exepath; 
    psiProcInfo.Arguments = strFFCMD;   
    psiProcInfo.UseShellExecute = false;  
    psiProcInfo.WindowStyle = ProcessWindowStyle.Hidden;  
    psiProcInfo.RedirectStandardError = true;    
    psiProcInfo.RedirectStandardOutput = true;   
    psiProcInfo.CreateNoWindow = true;     
    prcFFMPEG.StartInfo = psiProcInfo;   
    prcFFMPEG.Start(); 
    ffReader = prcFFMPEG.StandardError; 

    do 
    { 
     if (Bgw1.CancellationPending) 
     { 
      return; 
     } 
     Button5.Enabled = true; 
     Button3.Enabled = false; 
     strFFOUT = ffReader.ReadLine(); 
     RichTextBox1.Text = strFFOUT;     
     if (strFFOUT.Contains("frame=")) 
     { 
      currentFramestr = strFFOUT.Substring(7, 6); 
      currentFramestr = currentFramestr.Trim(); 
      currentFrameInt = System.Convert.ToInt32(currentFramestr, 16); 
     } 
     string percentage = System.Convert.ToInt32((ProgressBar1.Value/ProgressBar1.Maximum * 100)).ToString() + "%"; 
     ProgressBar1.Maximum = FCount + 1000; 
     ProgressBar1.Value = (currentFrameInt); 
     Label12.Text = "Current Encoded Frame: " + currentFrameInt; 
     Label11.Text = percentage; 
    } while (!(prcFFMPEG.HasExited || string.IsNullOrEmpty(strFFOUT))); 
} 
+0

您可以使用tryparse或捕獲錯誤並告訴用戶輸入垃圾?取決於你的應用程序最適合你的效果。 – BugFinder

+1

9f不是Convert.ToInt32可以處理的東西。不是一個整數(除非是基數16)。您是否對9(或可以轉換爲整數的任何初始字符)感興趣?或者您想僅告訴用戶輸入錯誤? – Steve

+2

或者您是否期望它將該解釋爲十六進制?領先空間怎麼樣?基本上,你會期望*得到什麼樣的價值? –

回答

1

你可以使用正則表達式的表達式來從你輸入字符串的開始提取任何連續的數字位數

.... 
currentFramestr = strFFOUT.Substring(7, 6).Trim(); 
Regex rx = new Regex(@"^\d+"); 
Match m = rx.Match(currentFramestr); 
if(m.Success) 
{ 
    int currentFrameInt = System.Convert.ToInt32(m.Value); 
    .... 
} 

爲避免你應該嘗試使用它的返回值前添加上的ReadLine一查NullReferenceException異常

do 
{ 
    .... 
    strFFOUT = ffReader.ReadLine(); 
    if(!string.IsNullOrEmpty(strFFOOUT)) 
    { 
     RichTextBox1.Text = strFFOUT;     
     .... 
    } 
    else 
    { 
     ... end of data? break if you want to exit 
     // break; 
    } 
} while (!prcFFMPEG.HasExited); 
+0

ffReader.ReadLine();當沒有更多數據要從打開的文件讀取時返回null。你需要在調用ReadLine之後檢查這個 – Steve

2

,如果它被認爲是一個十六進制數你給的功能基作爲第二個參數

啊,並擺脫了空間的這樣做第一:

//Option 1: this removes all spaces before and after the string 
currentFramestr = currentFramestr.Trim(); 

// Option 2: this removes all spaces in the string 
Regex.Replace(currentFramestr, @"\s+", ""); 

// this will convert a hexadecimal number from string to int 
currentFrameInt = System.Convert.ToInt32(currentFramestr, 16); 

爲第二個選項工作你需要包括:

using System.Text.RegularExpressions; 

第二個選項是從here

+0

我在第二行收到異常currentFrameInt = System.Convert.ToInt32(currentFramestr,16);例外情況是:其他不可分析的字符位於字符串的末尾。而我在currentFramestr中看到的值是:9f和9f有一個空格。 –

+0

我編輯了我的答案。如果你的字符串看起來如此變化,那麼你應該測試它是否是一個數字 –