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, 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.
i have an int which would be converted to a string (containing the numbers binary representation )Well, you either have a BINARY representation of the number, in an int or some such. Or you have a string representation,
r by context !in which case you need to know what base the number is (by prefix or implicitly by context or some such).
( if i got you right!)
Highlight Your Codes
The Boost C++ Libraries (online Reference)
Bill Bryson"...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.."
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.
tanx, right here !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!if you have an integer, and you set it x = 12, then x has the value 12 decimal. It is also 14 octal
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)
Bill Bryson"...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.."
you cannot conver integer between that and this...to convert between octal and decimals
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
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).
I may have misunderstood completely what you want to do, but I hope I'm actually helpingCode:strinstream ss; int n; ss << "12"; ss >> oct >> n; cout << "n = " << n;
--
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.
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)
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!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); }
just like what i do above or below:
( 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?Code:string DectoOct(int dec) { std::stringstream stream; stream <<std::oct<<dec; return stream.str(); }
Last edited by Masterx; 01-29-2009 at 06:14 AM.
Highlight Your Codes
The Boost C++ Libraries (online Reference)
Bill Bryson"...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.."
10 i would say (octal to decimal! )
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
wont work simply !Code:strinstream ss; int n; ss << "12"; ss >> dec >> n; cout << "n = " << n;
Highlight Your Codes
The Boost C++ Libraries (online Reference)
Bill Bryson"...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.."
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
yeah ,exactly , i wrote instead :
just doesnt work!from octal to decimal !!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();
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)
Bill Bryson"...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.."
Highlight Your Codes
The Boost C++ Libraries (online Reference)
Bill Bryson"...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.."
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)
Bill Bryson"...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.."
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.
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