Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. int treated as binary no.

int treated as binary no.

Scheduled Pinned Locked Moved C / C++ / MFC
c++testingbeta-testinghelpquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Manmohan29
    wrote on last edited by
    #1

    void CaDlg::OnBnClickedBrefreshdevices()
    {
    // TODO: Add your control notification handler code here
    m_ctlStatus.SetString(_T("Not Functional"));
    UpdateData(FALSE);
    unsigned int field0 = 0, field1 = 0, field2 = 0, field3 = 0;
    CString ips = _T("");
    char a[4] = "";
    int t=0,c=0;

    for(field0 = 192; field0 <= 192; field0++)
    {
    	for(field1 = 168; field1 <= 168; field1++)
    	{
    		for(field2 = 0; field2 <= 255; field2++)
    		{
    			for(field3 = 0; field3 <= 255; field3++)
    			{
    				
    				\_itoa\_s(field0, a, 4, 10);
    				ips += a;
    				ips += '.';
    				\_itoa\_s(field1, a, 4, 10);
    				ips += a;
    				ips += '.';
    				\_itoa\_s(field2, a, 4, 10);
    				ips += a;
    				ips += '.';
    				\_itoa\_s(field3, a, 4, 10);
    				ips += a;
    				t = LCDevices.InsertItem(c, ips);
    				c++;                   // some counter variable
    
    				LCDevices.SetItemText(c, 1, \_T("Testing"));
    				ips = \_T("");          //Reset sting variable
    
    			}
    		}
    	}
    }
    

    }

    In this code I am getting a bug when the value of t reaches 9. When InsertItem(c, ips); increases the value to 10 (ten) it becomes 2 (i.e. 10 is treated as binary nos. 1 0). Why is it so? the integer c is not treated as binary no.

    Future Lies in Present. Manmohan Bishnoi

    D 1 Reply Last reply
    0
    • M Manmohan29

      void CaDlg::OnBnClickedBrefreshdevices()
      {
      // TODO: Add your control notification handler code here
      m_ctlStatus.SetString(_T("Not Functional"));
      UpdateData(FALSE);
      unsigned int field0 = 0, field1 = 0, field2 = 0, field3 = 0;
      CString ips = _T("");
      char a[4] = "";
      int t=0,c=0;

      for(field0 = 192; field0 <= 192; field0++)
      {
      	for(field1 = 168; field1 <= 168; field1++)
      	{
      		for(field2 = 0; field2 <= 255; field2++)
      		{
      			for(field3 = 0; field3 <= 255; field3++)
      			{
      				
      				\_itoa\_s(field0, a, 4, 10);
      				ips += a;
      				ips += '.';
      				\_itoa\_s(field1, a, 4, 10);
      				ips += a;
      				ips += '.';
      				\_itoa\_s(field2, a, 4, 10);
      				ips += a;
      				ips += '.';
      				\_itoa\_s(field3, a, 4, 10);
      				ips += a;
      				t = LCDevices.InsertItem(c, ips);
      				c++;                   // some counter variable
      
      				LCDevices.SetItemText(c, 1, \_T("Testing"));
      				ips = \_T("");          //Reset sting variable
      
      			}
      		}
      	}
      }
      

      }

      In this code I am getting a bug when the value of t reaches 9. When InsertItem(c, ips); increases the value to 10 (ten) it becomes 2 (i.e. 10 is treated as binary nos. 1 0). Why is it so? the integer c is not treated as binary no.

      Future Lies in Present. Manmohan Bishnoi

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Manmohan29 wrote:

      _itoa_s(field0, a, 4, 10); ips += a;

      You could change this to:

      ips = a;

      and eliminate the "reset" at the bottom of the loop.

      Manmohan29 wrote:

      LCDevices.SetItemText(c, 1, _T("Testing"));

      This should probably be:

      LCDevices.SetItemText(t, 1, _T("Testing"));

      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      M 1 Reply Last reply
      0
      • D David Crow

        Manmohan29 wrote:

        _itoa_s(field0, a, 4, 10); ips += a;

        You could change this to:

        ips = a;

        and eliminate the "reset" at the bottom of the loop.

        Manmohan29 wrote:

        LCDevices.SetItemText(c, 1, _T("Testing"));

        This should probably be:

        LCDevices.SetItemText(t, 1, _T("Testing"));

        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        M Offline
        M Offline
        Manmohan29
        wrote on last edited by
        #3

        I did what you told but values after inserting a breakpoint after

        c++;

        ips "192.168.0.9" t 9 c 10 ips "192.168.0.10" t 2 c 11 the problem is still there.:confused:

        Future Lies in Present. Manmohan Bishnoi

        D 1 Reply Last reply
        0
        • M Manmohan29

          I did what you told but values after inserting a breakpoint after

          c++;

          ips "192.168.0.9" t 9 c 10 ips "192.168.0.10" t 2 c 11 the problem is still there.:confused:

          Future Lies in Present. Manmohan Bishnoi

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          I'm not sure what you are asking or showing. When you call InsertItem(), it will return the index of the item that was added. This index is used in subsequent calls to SetItemText(). If you have anything other than that, your code will not work correctly. Feel free to show the updated code.

          "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          M 1 Reply Last reply
          0
          • D David Crow

            I'm not sure what you are asking or showing. When you call InsertItem(), it will return the index of the item that was added. This index is used in subsequent calls to SetItemText(). If you have anything other than that, your code will not work correctly. Feel free to show the updated code.

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            M Offline
            M Offline
            Manmohan29
            wrote on last edited by
            #5

            void CaDlg::OnBnClickedBrefreshdevices()
            {
            // TODO: Add your control notification handler code here
            m_ctlStatus.SetString(_T("Not Functional"));
            UpdateData(FALSE);
            unsigned int field0 = 0, field1 = 0, field2 = 0, field3 = 0;
            CString ips = _T("");
            char a[4] = "";
            int t=0,c=0;

            for(field0 = 192; field0 <= 192; field0++)
            {
            	for(field1 = 168; field1 <= 168; field1++)
            	{
            		for(field2 = 0; field2 <= 255; field2++)
            		{
            			for(field3 = 0; field3 <= 255; field3++)
            			{
            				//ips.Format(field0 + "." + field1 + "." + field2 + "." + field3);
            				\_itoa\_s(field0, a, 4, 10);
            				ips = a;
            				ips += '.';
            				\_itoa\_s(field1, a, 4, 10);
            				ips += a;
            				ips += '.';
            				\_itoa\_s(field2, a, 4, 10);
            				ips += a;
            				ips += '.';
            				\_itoa\_s(field3, a, 4, 10);
            				ips += a;
            				t = LCDevices.InsertItem(c, ips);
            				c++;
            
            				LCDevices.SetItemText(t, 1, \_T("Testing"));
            
            			}
            		}
            	}
            }
            

            }

            I am using index in subsequent calls to SetItemText(). but still when t = 10 then it becomes t = 2 (binary 10).:confused: one more thing the counting is like this:- ips "192.168.0.0" t = 0 . . . ips "192.168.0.9" t = 9 ips "192.168.0.10" t = 2 . . . ips "192.168.0.20" t = 13 . . . ips "192.168.0.99" t = 99 ips "192.168.0.100" t = 3 ips "192.168.0.101" t = 4 ips "192.168.0.102" t = 5

            Future Lies in Present. Manmohan Bishnoi

            D 1 Reply Last reply
            0
            • M Manmohan29

              void CaDlg::OnBnClickedBrefreshdevices()
              {
              // TODO: Add your control notification handler code here
              m_ctlStatus.SetString(_T("Not Functional"));
              UpdateData(FALSE);
              unsigned int field0 = 0, field1 = 0, field2 = 0, field3 = 0;
              CString ips = _T("");
              char a[4] = "";
              int t=0,c=0;

              for(field0 = 192; field0 <= 192; field0++)
              {
              	for(field1 = 168; field1 <= 168; field1++)
              	{
              		for(field2 = 0; field2 <= 255; field2++)
              		{
              			for(field3 = 0; field3 <= 255; field3++)
              			{
              				//ips.Format(field0 + "." + field1 + "." + field2 + "." + field3);
              				\_itoa\_s(field0, a, 4, 10);
              				ips = a;
              				ips += '.';
              				\_itoa\_s(field1, a, 4, 10);
              				ips += a;
              				ips += '.';
              				\_itoa\_s(field2, a, 4, 10);
              				ips += a;
              				ips += '.';
              				\_itoa\_s(field3, a, 4, 10);
              				ips += a;
              				t = LCDevices.InsertItem(c, ips);
              				c++;
              
              				LCDevices.SetItemText(t, 1, \_T("Testing"));
              
              			}
              		}
              	}
              }
              

              }

              I am using index in subsequent calls to SetItemText(). but still when t = 10 then it becomes t = 2 (binary 10).:confused: one more thing the counting is like this:- ips "192.168.0.0" t = 0 . . . ips "192.168.0.9" t = 9 ips "192.168.0.10" t = 2 . . . ips "192.168.0.20" t = 13 . . . ips "192.168.0.99" t = 99 ips "192.168.0.100" t = 3 ips "192.168.0.101" t = 4 ips "192.168.0.102" t = 5

              Future Lies in Present. Manmohan Bishnoi

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              While not related to your problem (at least I don't think so), you could shorten it down a bit to:

              int c = 0;

              for (unsigned int field2 = 0; field2 <= 255; field2++)
              {
              for (unsigned int field3 = 0; field3 <= 255; field3++)
              {
              CString ips;
              ips.Format(_T("192.168.%u.%u"), field2, field3); // you may need to use %03u to avoid potential sorting issues

                  int nIndex = LCDevices.InsertItem(c, ips);
                  LCDevices.SetItemText(nIndex, 1, \_T("Testing"));
              
                  c++;
              }
              

              }

              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups