-1

The line:

currentFrameInt =  System.Convert.ToInt32(currentFramestr);

In this case i see in currentFramestr the value " 9f" Including three spaces in the start of the string.

The currentFramestr is part of updating a 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);

What i want to do is to update the progressBar value.

Maybe the whole method code will give more clue of what i want and the using with currentFramestr. In general i want to update the progressBar1 value.

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)));
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • You can use tryparse or capture the error and tell the user off for entering rubbish?? depending on what you feel works best with your app.. – BugFinder Jun 09 '16 at 12:19
  • 1
    9f is not something the Convert.ToInt32 could handle. Is not an integer (unless base 16). Are you interested in the 9 (or any initial character that could be converted to an integer) or do you want to simply tell your user that the input is wrong? – Steve Jun 09 '16 at 12:21
  • 2
    Or are you expecting it to interpret that as hex? What about the leading spaces? Basically, what value do you *expect* to get out? – Jon Skeet Jun 09 '16 at 12:25
  • What i want to do is using the currentFramestr with updating a progressBar1 value progress. Updated my question. – Sharon Gabriel Jun 09 '16 at 13:24
  • for problems like "how to remove blanks from string" and "how to test whether string is a number" there are already solution on StackOverflow. One just has to search a little :) – Mong Zhu Jun 09 '16 at 13:33

2 Answers2

2

if it is supposed to be a hexadecimal number you give the function the base as the second parameter

ah and to get rid of the spaces do this first:

//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);

for the second option to work you need to include:

using System.Text.RegularExpressions;

second option is from here

Community
  • 1
  • 1
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • I'm getting exception on the second line currentFrameInt = System.Convert.ToInt32(currentFramestr, 16); the exception is: Additional non-parsable characters are at the end of the string. And i see in currentFramestr the value: 9 f there is a space beteen the 9 and the f. – Sharon Gabriel Jun 09 '16 at 13:21
  • I edited my answer. If your string seems to be so variable you should test it whether it is a number at all – Mong Zhu Jun 09 '16 at 13:30
1

You could use a Regex expression to extract any consecutive numeric digit from the start of your input string

....
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);
    ....
}

To avoid the NullReferenceException you should add a check on ReadLine before trying to use its return value

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);
Steve
  • 213,761
  • 22
  • 232
  • 286
  • ffReader.ReadLine(); returns null when there is no more data to read from the opened file. You need to check for this after calling ReadLine – Steve Jun 09 '16 at 13:40