none
FormatException was unhandled RRS feed

  • Question

  • Kindly Advise us 

    i am received string from Serial Port *0000.000000.000000.00000.

    MCV Value is = *0000

    string MCV = DataIN.Substring(50, 5);
    decimal mcvValue = Convert.ToDecimal(MCV);
                               
    MCV = (mcvValue / 100).ToString();

    Please help me to to replace from "*"   to "0"

    and then perform calculation 

    MCV = (mcvValue / 100).ToString();

    Thursday, March 1, 2018 6:41 PM

Answers

  • Hello saqsaqPK,

    Try to use String.Replace method.

                string MCV = DataIN.Substring(0, 5).Replace('*','0');
    
                decimal mcvValue = Convert.ToDecimal(MCV);
    
                MCV = (mcvValue / 100).ToString();

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by saqsaqPK Friday, March 2, 2018 3:29 PM
    Friday, March 2, 2018 5:50 AM
    Moderator

All replies

  • There are many problems there. First, the Substring says to start in position 50 but DataIN is only 25 characters big. So the following should work.

    StringBuilder sb = new StringBuilder(DataIN.Substring(0, 5));
    if (sb[0] == '*')
        sb[0] = '0';
    string MCV = sb.ToString();
    decimal mcvValue = Convert.ToDecimal(MCV);
    MCV = (mcvValue / 100).ToString();

    Note that someone else asked nearly the same question about a hour after this one; see FormatException was unhandled.



    Sam Hobbs
    SimpleSamples.Info


    Thursday, March 1, 2018 8:59 PM
  • Hello saqsaqPK,

    Try to use String.Replace method.

                string MCV = DataIN.Substring(0, 5).Replace('*','0');
    
                decimal mcvValue = Convert.ToDecimal(MCV);
    
                MCV = (mcvValue / 100).ToString();

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by saqsaqPK Friday, March 2, 2018 3:29 PM
    Friday, March 2, 2018 5:50 AM
    Moderator
  • Thank you Problem Solved
    Friday, March 2, 2018 3:29 PM