Thread: help on debugging

  1. #31
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, you either have a BINARY representation of the number, in an int or some such. Or you have a string representation, in which case you need to know what base the number is (by prefix or implicitly by context or some such).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #32
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by matsp View Post
    Well, you either have a BINARY representation of the number, in an int or some such. Or you have a string representation, in which case you need to know what base the number is (by prefix or implicitly by context or some such).

    --
    Mats
    Well, you either have a BINARY representation of the number, in an int or some such. Or you have a string representation,
    i have an int which would be converted to a string (containing the numbers binary representation )
    in which case you need to know what base the number is (by prefix or implicitly by context or some such).
    r by context !
    ( if i got you right!)
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  3. #33
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, let's try to explain in a different way.

    Right, so if you have an integer, and you set it x = 12, then x has the value 12 decimal. It is also 14 octal, 0x0C in hex, 1100 in binary and 10 in base 12.

    If you have a string with the number '1', '2' in it, it would depend on what the base is, what the value of the number is.
    Here's a list of base vs. decimal:
    Code:
    base value
    10   12
    3    5
    8    10
    16   18
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #34
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by matsp View Post
    So, let's try to explain in a different way.

    Right, so if you have an integer, and you set it x = 12, then x has the value 12 decimal. It is also 14 octal, 0x0C in hex, 1100 in binary and 10 in base 12.

    If you have a string with the number '1', '2' in it, it would depend on what the base is, what the value of the number is.
    Here's a list of base vs. decimal:
    Code:
    base value
    10   12
    3    5
    8    10
    16   18
    --
    Mats
    tanx, right here !
    if you have an integer, and you set it x = 12, then x has the value 12 decimal. It is also 14 octal
    yeah this is my problem , i know about strings , and i do understand what you mean .when there is a string containing "123" or stuff if it can be in any base!
    this is straight forward!
    but when i have an integer that simultaneously can be interpreted as"decimal" and again as Octal, how can i just make the difference and make it understandable for the stringstream function which has the duty to convert between octal and decimals ! could i convey what i wanted to say ?
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  5. #35
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    to convert between octal and decimals
    you cannot conver integer between that and this...

    integer is always stored as binary...

    what you could do - to take integer that contains say 12 and represent it in string as
    "12" or "014" or "0xC"

    That is what you are trying to do?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #36
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you have an integer, then the value is stored in binary. Most often, however, we present that binary number as a decimal, hex or octal number because humans tend to get confused with very large numbers - and a relatively small number of 146 is still an 8-digit number in binary, so it's not that easy to read even for such a limited number.

    You can make streams use octal for input or output (e.g. if your stringstream contains the letter '1', '2', you can read that into an integer and get it to have the binary value 1010, or which would be 10 decimal).
    Code:
    strinstream ss;
    int n;
    ss << "12";
    ss >> oct >> n;
    cout << "n = " << n;
    I may have misunderstood completely what you want to do, but I hope I'm actually helping

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #37
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by vart View Post
    you cannot conver integer between that and this...

    integer is always stored as binary...

    what you could do - to take integer that contains say 12 and represent it in string as
    "12" or "014" or "0xC"

    That is what you are trying to do?
    well , im trying to make a better counter part for this function with the help of stringstream or any other standard function (except boost , cause i dont have access to it)
    Code:
    int OcttoDec(int oct)
    {
            int n,r,s=0,i;
            n=oct;
            double S=0;
            for(i=0;n!=0;i++)
             {
                     r=n%10;
                     s=s + r * ((pow(8.0,i)));
                     n=n/10;
             }
            return static_cast<int>(s);
    }
    as you see, im seeking a way to convert an integer Octal value to an integer decimal value ! . or convert an integer octal value to a decimal string!

    just like what i do above or below:
    Code:
    string DectoOct(int dec)
    {
        std::stringstream stream;
        stream <<std::oct<<dec;
        return stream.str();
    
    }
    ( if i have it in string , big deal i can convert it to integer using my state of the art Atoi() function . so it doesnt make any differecen though .! but i prefer to have sth like the second function for conversion , , did i convey it completley now?
    Last edited by Masterx; 01-29-2009 at 06:14 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  8. #38
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by vart View Post
    you cannot conver integer between that and this...

    integer is always stored as binary...

    what you could do - to take integer that contains say 12 and represent it in string as
    "12" or "014" or "0xC"

    That is what you are trying to do?
    10 i would say (octal to decimal! )
    Quote Originally Posted by matsp View Post
    If you have an integer, then the value is stored in binary. Most often, however, we present that binary number as a decimal, hex or octal number because humans tend to get confused with very large numbers - and a relatively small number of 146 is still an 8-digit number in binary, so it's not that easy to read even for such a limited number.

    You can make streams use octal for input or output (e.g. if your stringstream contains the letter '1', '2', you can read that into an integer and get it to have the binary value 1010, or which would be 10 decimal).
    Code:
    strinstream ss;
    int n;
    ss << "12";
    ss >> oct >> n;
    cout << "n = " << n;
    I may have misunderstood completely what you want to do, but I hope I'm actually helping

    --
    Mats
    many tanx dear Mats, this would be very useful when i have the number in decimal, but the reverse is not true , meaning i fthe number stored in integer is 12, i must be treated as octal! so when it is octal , and i use sush a thing , this would just simply act as if it was decimal! ( this is exactly my problem , just this! the reverse of the example wont work.) meaning
    Code:
    strinstream ss;
    int n;
    ss << "12";
    ss >> dec >> n;
    cout << "n = " << n;
    wont work simply !
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  9. #39
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you mean like this?
    Code:
    int OcttoDec(int y)
    {
        std::stringstream test;
        test << y;
    
        int x;
        test >> std::oct >> x;
        return x;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #40
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by vart View Post
    you mean like this?
    Code:
    int OcttoDec(int y)
    {
        std::stringstream test;
        test << y;
    
        int x;
        test >> std::oct >> x;
        return x;
    }
    yeah ,exactly , i wrote instead :
    Code:
        stringstream ss;
        ss << num; //get the number ,( if you think this is a decimal number, ), 
        ss>>oct>>num; //save it as octal 
        
        ss<<dec<<num;//then retrieve it as decimal
        ss<< num;//palce it in num again 
        cout<<ss.str();
    just doesnt work!from octal to decimal !!
    by the way your code is working just fine ! great, but mine ! no, why ?
    Last edited by Masterx; 01-29-2009 at 06:51 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  11. #41
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Masterx View Post
    by the way your code is working just fine ! great, but mine ! no, why ?
    Just compare line by line

    ss<< num;//palce it in num again
    comment specifies opposit action to the made by code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #42
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by vart View Post
    Just compare line by line


    comment specifies opposit action to the made by code
    tanx, about the comment, its just a mistake , ( i changed the code , but didnt remove the comment) .

    so comparing the lines with each other, i just did the reverse huh ! ?
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  13. #43
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    now, how an i convert an octal to binary using stringstream? is it even possible ?
    ive posted my octal to binary function already (here it is again) , is there any better function doing the job?
    Code:
    string OcttoBin(int n)
    {
    
    
             int a[6],i=0,t=0,num;
             string str,str1,str2,message;
    
             char buffer[9];
    
             string str_array[9];
             const size_t str_array_size = sizeof(str_array) / sizeof(str_array[0]);
    
             while(n!=0)
             {
                     a[i]=n%10;
                     n=n/10;
                     if(a[i]>7)
                     t=1;
                     i++;
             }
             i--;
             if(t==0)
              for(;i>=0;i--)
              {
                      switch(a[i])
                       {
                               case 0:
                               if (!str.empty())
                               str.append("000");
                               else
                               str="000";
                               break;
    
                               case 1:
                               if (!str.empty())
                               str.append("001");
                               else
                               str="001";
                               break;
    
                               case 2:
                               if (!str.empty())
                               str.append("010");
                               else
                               str="010";
                               break;
    
                               case 3:
                               if (!str.empty())
                               str.append("011");
                               else
                               str="011";
                               break;
    
                               case 4:
                               if (!str.empty())
                               str.append("100");
                               else
                               str="100";
                               break;
    
                               case 5:
                               if (!str.empty())
                               str.append("101");
                               else
                               str="101";
                               break;
    
                               case 6:
                               if (!str.empty())
                               str.append("110");
                               else
                               str="110";
                               break;
    
                               case 7:
                               if (!str.empty())
                               str.append("111");
                               else
                               str="111";
                               break;
                       }
              }
              str1="000000";
              str2="000";
                  if (str.length()==3)
                  {
                            str1.append(str);
                            str=str1;
                  }
    
                  if (str.length()==6)
                  {
                            str2.append(str);
                            str=str2;
                  }
    
    
    
             if(t==1)
              {
    
                      message="no";
                    // cout<<"Not a Octal number\n";
                     return message;
              }
    
    return str;
    }
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  14. #44
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm even more confused. If you have a integer, it is stored in binary in the computer. If it prints as 12, 14, 0x0c or something else is presentation. If you have a number you want to present in octal, then use the oct. You can not "convert an integer in decimal to octal", because integers are stored internally in the computer as binary. If you want to display an integer as binary, then do this:
    Code:
    std::string result;
    int x = 12;  // Or 012 if you want it to be octal. 0x12 for hex. 
    const int numdigits = 10;  // Number of bits you want to display. 
    for(i = 0; i < numdigits; i++)
    {
        result += ('0' + (x & 1)) + result;
        x >>= 1;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #45
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Something like
    Code:
    std::string OcttoBin2(int n)
    {
    	std::bitset<CHAR_BIT * sizeof(int)> bs(OcttoDec(n));
    
    	std::stringstream test;
    	test << bs;
    	return test.str();
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dev-C++: Problems with Breakpoint Debugging
    By Thileepan_Bala in forum C Programming
    Replies: 1
    Last Post: 01-17-2008, 10:48 AM
  2. Problem in debugging in Eclipse
    By Bargi in forum Linux Programming
    Replies: 1
    Last Post: 08-21-2007, 09:53 AM
  3. Debugging Dev C++
    By tuurb046 in forum Tech Board
    Replies: 10
    Last Post: 08-16-2007, 12:51 PM
  4. Debugging book recommendation
    By dagans in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 09-13-2005, 07:35 PM
  5. debugging directx apps
    By confuted in forum C++ Programming
    Replies: 1
    Last Post: 08-16-2003, 08:56 AM