How to read character by character right to left?












0














I hope and you can help me please, I have a label that shows a value in binary.



Example: "1000010101", this data, I'm reading it with an inverted for. That is, starting from right to left, the binary number is dynamic, so it will not always be the same.



Until now this is my idea, but it does not give me any value



for (int i = lbl_conversion.Text.Length; i > 0; i--)
{
if (st[i] == 1)
{
MessageBox("1");
}
else
{
MessageBox("0");
}

}


What I would like is to read character by character from left to right and know if it is "1" or "0" and then make a comparison, could someone support me to get that result?



Thank you.










share|improve this question




















  • 4




    int i = lbl_conversion.Text.Length - 1; please, note -1; i >= 0
    – Dmitry Bychenko
    Nov 12 '18 at 19:14








  • 1




    You should be getting an exception, which should be providing you a big clue about what you're doing wrong here.
    – Clockwork-Muse
    Nov 12 '18 at 19:14










  • Even if you do get this working, I'm not sure how 10 separate message boxes are useful. What are you actually trying to do with this?
    – crashmstr
    Nov 12 '18 at 19:17










  • See also C# read string from back to end
    – Heretic Monkey
    Nov 12 '18 at 19:18










  • "and then make a comparison" comparing to what...?
    – DaniDev
    Nov 12 '18 at 19:39
















0














I hope and you can help me please, I have a label that shows a value in binary.



Example: "1000010101", this data, I'm reading it with an inverted for. That is, starting from right to left, the binary number is dynamic, so it will not always be the same.



Until now this is my idea, but it does not give me any value



for (int i = lbl_conversion.Text.Length; i > 0; i--)
{
if (st[i] == 1)
{
MessageBox("1");
}
else
{
MessageBox("0");
}

}


What I would like is to read character by character from left to right and know if it is "1" or "0" and then make a comparison, could someone support me to get that result?



Thank you.










share|improve this question




















  • 4




    int i = lbl_conversion.Text.Length - 1; please, note -1; i >= 0
    – Dmitry Bychenko
    Nov 12 '18 at 19:14








  • 1




    You should be getting an exception, which should be providing you a big clue about what you're doing wrong here.
    – Clockwork-Muse
    Nov 12 '18 at 19:14










  • Even if you do get this working, I'm not sure how 10 separate message boxes are useful. What are you actually trying to do with this?
    – crashmstr
    Nov 12 '18 at 19:17










  • See also C# read string from back to end
    – Heretic Monkey
    Nov 12 '18 at 19:18










  • "and then make a comparison" comparing to what...?
    – DaniDev
    Nov 12 '18 at 19:39














0












0








0







I hope and you can help me please, I have a label that shows a value in binary.



Example: "1000010101", this data, I'm reading it with an inverted for. That is, starting from right to left, the binary number is dynamic, so it will not always be the same.



Until now this is my idea, but it does not give me any value



for (int i = lbl_conversion.Text.Length; i > 0; i--)
{
if (st[i] == 1)
{
MessageBox("1");
}
else
{
MessageBox("0");
}

}


What I would like is to read character by character from left to right and know if it is "1" or "0" and then make a comparison, could someone support me to get that result?



Thank you.










share|improve this question















I hope and you can help me please, I have a label that shows a value in binary.



Example: "1000010101", this data, I'm reading it with an inverted for. That is, starting from right to left, the binary number is dynamic, so it will not always be the same.



Until now this is my idea, but it does not give me any value



for (int i = lbl_conversion.Text.Length; i > 0; i--)
{
if (st[i] == 1)
{
MessageBox("1");
}
else
{
MessageBox("0");
}

}


What I would like is to read character by character from left to right and know if it is "1" or "0" and then make a comparison, could someone support me to get that result?



Thank you.







c# string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 19:27









Dmitry Bychenko

106k992132




106k992132










asked Nov 12 '18 at 19:12









Ric_hcRic_hc

1608




1608








  • 4




    int i = lbl_conversion.Text.Length - 1; please, note -1; i >= 0
    – Dmitry Bychenko
    Nov 12 '18 at 19:14








  • 1




    You should be getting an exception, which should be providing you a big clue about what you're doing wrong here.
    – Clockwork-Muse
    Nov 12 '18 at 19:14










  • Even if you do get this working, I'm not sure how 10 separate message boxes are useful. What are you actually trying to do with this?
    – crashmstr
    Nov 12 '18 at 19:17










  • See also C# read string from back to end
    – Heretic Monkey
    Nov 12 '18 at 19:18










  • "and then make a comparison" comparing to what...?
    – DaniDev
    Nov 12 '18 at 19:39














  • 4




    int i = lbl_conversion.Text.Length - 1; please, note -1; i >= 0
    – Dmitry Bychenko
    Nov 12 '18 at 19:14








  • 1




    You should be getting an exception, which should be providing you a big clue about what you're doing wrong here.
    – Clockwork-Muse
    Nov 12 '18 at 19:14










  • Even if you do get this working, I'm not sure how 10 separate message boxes are useful. What are you actually trying to do with this?
    – crashmstr
    Nov 12 '18 at 19:17










  • See also C# read string from back to end
    – Heretic Monkey
    Nov 12 '18 at 19:18










  • "and then make a comparison" comparing to what...?
    – DaniDev
    Nov 12 '18 at 19:39








4




4




int i = lbl_conversion.Text.Length - 1; please, note -1; i >= 0
– Dmitry Bychenko
Nov 12 '18 at 19:14






int i = lbl_conversion.Text.Length - 1; please, note -1; i >= 0
– Dmitry Bychenko
Nov 12 '18 at 19:14






1




1




You should be getting an exception, which should be providing you a big clue about what you're doing wrong here.
– Clockwork-Muse
Nov 12 '18 at 19:14




You should be getting an exception, which should be providing you a big clue about what you're doing wrong here.
– Clockwork-Muse
Nov 12 '18 at 19:14












Even if you do get this working, I'm not sure how 10 separate message boxes are useful. What are you actually trying to do with this?
– crashmstr
Nov 12 '18 at 19:17




Even if you do get this working, I'm not sure how 10 separate message boxes are useful. What are you actually trying to do with this?
– crashmstr
Nov 12 '18 at 19:17












See also C# read string from back to end
– Heretic Monkey
Nov 12 '18 at 19:18




See also C# read string from back to end
– Heretic Monkey
Nov 12 '18 at 19:18












"and then make a comparison" comparing to what...?
– DaniDev
Nov 12 '18 at 19:39




"and then make a comparison" comparing to what...?
– DaniDev
Nov 12 '18 at 19:39












2 Answers
2






active

oldest

votes


















2














Since strings are zero based, correct for loop will be



   // Note "- 1" and ">=" 
for (int i = lbl_conversion.Text.Length - 1; i >= 0; --i)
if (st[i] == '1') //DONE: comparing with character, not integer
{
MessageBox("1");
}
else
{
MessageBox("0");
}


Or you can just Reverse the string (with a help of Linq):



   // Let's read each character in reversed order
foreach (char c in lbl_conversion.Text.Reverse())
MessageBox(c.ToString());





share|improve this answer





















  • Excellent answer Dmity, Thanks...
    – Ric_hc
    Nov 12 '18 at 20:37



















0














just one more (similar to prev posted)



string s="10111011";
s.Reverse().ToList().ForEach(ch=>Console.WriteLine(ch=='1'?"A":"0"));





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268627%2fhow-to-read-character-by-character-right-to-left%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Since strings are zero based, correct for loop will be



       // Note "- 1" and ">=" 
    for (int i = lbl_conversion.Text.Length - 1; i >= 0; --i)
    if (st[i] == '1') //DONE: comparing with character, not integer
    {
    MessageBox("1");
    }
    else
    {
    MessageBox("0");
    }


    Or you can just Reverse the string (with a help of Linq):



       // Let's read each character in reversed order
    foreach (char c in lbl_conversion.Text.Reverse())
    MessageBox(c.ToString());





    share|improve this answer





















    • Excellent answer Dmity, Thanks...
      – Ric_hc
      Nov 12 '18 at 20:37
















    2














    Since strings are zero based, correct for loop will be



       // Note "- 1" and ">=" 
    for (int i = lbl_conversion.Text.Length - 1; i >= 0; --i)
    if (st[i] == '1') //DONE: comparing with character, not integer
    {
    MessageBox("1");
    }
    else
    {
    MessageBox("0");
    }


    Or you can just Reverse the string (with a help of Linq):



       // Let's read each character in reversed order
    foreach (char c in lbl_conversion.Text.Reverse())
    MessageBox(c.ToString());





    share|improve this answer





















    • Excellent answer Dmity, Thanks...
      – Ric_hc
      Nov 12 '18 at 20:37














    2












    2








    2






    Since strings are zero based, correct for loop will be



       // Note "- 1" and ">=" 
    for (int i = lbl_conversion.Text.Length - 1; i >= 0; --i)
    if (st[i] == '1') //DONE: comparing with character, not integer
    {
    MessageBox("1");
    }
    else
    {
    MessageBox("0");
    }


    Or you can just Reverse the string (with a help of Linq):



       // Let's read each character in reversed order
    foreach (char c in lbl_conversion.Text.Reverse())
    MessageBox(c.ToString());





    share|improve this answer












    Since strings are zero based, correct for loop will be



       // Note "- 1" and ">=" 
    for (int i = lbl_conversion.Text.Length - 1; i >= 0; --i)
    if (st[i] == '1') //DONE: comparing with character, not integer
    {
    MessageBox("1");
    }
    else
    {
    MessageBox("0");
    }


    Or you can just Reverse the string (with a help of Linq):



       // Let's read each character in reversed order
    foreach (char c in lbl_conversion.Text.Reverse())
    MessageBox(c.ToString());






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 '18 at 19:18









    Dmitry BychenkoDmitry Bychenko

    106k992132




    106k992132












    • Excellent answer Dmity, Thanks...
      – Ric_hc
      Nov 12 '18 at 20:37


















    • Excellent answer Dmity, Thanks...
      – Ric_hc
      Nov 12 '18 at 20:37
















    Excellent answer Dmity, Thanks...
    – Ric_hc
    Nov 12 '18 at 20:37




    Excellent answer Dmity, Thanks...
    – Ric_hc
    Nov 12 '18 at 20:37













    0














    just one more (similar to prev posted)



    string s="10111011";
    s.Reverse().ToList().ForEach(ch=>Console.WriteLine(ch=='1'?"A":"0"));





    share|improve this answer


























      0














      just one more (similar to prev posted)



      string s="10111011";
      s.Reverse().ToList().ForEach(ch=>Console.WriteLine(ch=='1'?"A":"0"));





      share|improve this answer
























        0












        0








        0






        just one more (similar to prev posted)



        string s="10111011";
        s.Reverse().ToList().ForEach(ch=>Console.WriteLine(ch=='1'?"A":"0"));





        share|improve this answer












        just one more (similar to prev posted)



        string s="10111011";
        s.Reverse().ToList().ForEach(ch=>Console.WriteLine(ch=='1'?"A":"0"));






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 19:43









        AndrewFAndrewF

        333




        333






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268627%2fhow-to-read-character-by-character-right-to-left%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma