I used the method you guys said, I got it to work exept one small detail. It only saves the first number, aka the first loop-round..
stringstream ascii;
while(tWord[x] != '\0')
{
ans = int(tWord[x]);
toBinary(ans);
ascii << ans;
x++;
}
int result = ans;
cout << endl << endl << ans;
The whole application takes a string, converts to its ascii equevilants, converts the ascii number to binary. I want to write out not only the binary, but the ascii numbers..
"The word: BeatOne is
10010-blabla in binary
and 11547-blabla-some-more in asciicode."
C++, Don't quite get the result I expect...
Quote:Original post by BeatOne
When I convert from decimal to binary bases [...]
void toBinary(int dec)
You don't (and thus the name of your parameter is misleading). All you do is print the binary representation of a number. The number itself is NOT decimal. For example you could write:
toBinary(0x10);
toBinary(020);
And get the result 10000 onscreen. No need to write seperate toBinary methods for decimal, hexadecimal and octal. BTW the internal representation of numbers in a computer is already binary. Funny, huh? :)
There is no such thing as converting an integer variable (int, short, long, etc.) from base X to base Y. It is a variable which represents some value, and does so by having some pattern of bits. If I have fifty-four pennies, then I may write that as 54 pennies in decimal or 0x36 pennies in hex or 066 pennies in octal or 110110 pennies in binary, but I still have the same amount of money.
Instead, there are such things as:
a) outputting a text representation of a number in base Y.
b) inputting a number from what is assumed to be a text representation in base X.
Also, don't use char arrays to represent your text. Use std::string.
Instead, there are such things as:
a) outputting a text representation of a number in base Y.
b) inputting a number from what is assumed to be a text representation in base X.
Also, don't use char arrays to represent your text. Use std::string.
For the case of writing out the binary representation of an integer...
Boy isn't the standard library nice?
std::bitset<sizeof(int) * 8> bs(val);std::cout<<bs;
Boy isn't the standard library nice?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement