1. 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

2. Originally Posted by matsp
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!)

3. 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

4. Originally Posted by matsp
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 ?

5. 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?

6. 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

7. Originally Posted by vart
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?

8. Originally Posted by vart
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! )
Originally Posted by matsp
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 !

9. you mean like this?
Code:
int OcttoDec(int y)
{
std::stringstream test;
test << y;

int x;
test >> std::oct >> x;
return x;
}

10. Originally Posted by vart
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 ?

11. Originally Posted by Masterx
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

12. Originally Posted by vart
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 ! ?

13. 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;
}

14. 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

15. 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();
}