# Thread: Binary Converter - Various stupid string issues :p

1. ## Beginner Problem- Various stupid string issues :p

First of all i would just like to thank anyone who replies to this post, i feel very awkward asking for help as i prefer to solve it myself but, being very new to C++, i am having trouble with strings

I am just writing a simple program to convert a real number to binary and managed to get it working in a linear form. However, mostly for practice, partly for general organisation and for further development i decided to break it up into discrete functions. To output the binary at the end of the primary function (binary_conv) i decided to make a string with the binary digits within it. however, the output of the function is nonsensical and seems to be probably a value in the general memory. I am quite sure that return is probably not the right way to output a string but i am not aware of an alternative. I have used several cout s in my quest to solve this problem. I am sure my code is not the most elegant but is seems to get the job done Thanks again! I enclose the code below:

Code:
```#include <iostream>
#include <math.h>

using namespace std;
int digits(int number);
int binary_conv(int digits, int real_number);
int main()
{
for(int x= 0; x<=10; x++)
{
int real;
cout<<"Please enter a real number: ";
cin>>real;
int c=digits(real);
int binary=binary_conv(c,real);
cout<<binary <<"\n";

}
}

int digits(int number) /* calculates the number of digits needed, ie. 101 = 3 digits */
{
float b = (float) number;
int c = 0;
while(b>=1.0) /* simply divides by two until b is less than 1 and counts the number of divisions */
{
b=b/2.0;
c++;
}
return c;
}

int binary_conv(int digits, int real_number)
{
char *binary = new char[digits+1]; /* I am not at all sure of this part */
float b=(float)real_number; /* Convert the number into a float for processing */
while(digits>0) /* Processes the real number through the repitition of a simple sequence*/
{
b=b/(pow(2,digits-1)); /* Dividing the real number by an appropriate bit value (starting at the highest for the number and decreasing) determines the state of the bit*/
if(b>=1)
{
strcat(binary,"1"); /* If a number higher than one is returned the program outputs a 1 */
cout<<"1"; /* Used to check the byte before the function outputs it */
b=b-1;
}
else
{
strcat(binary,"0"); /*If the bit is too big the program outputs a 0 */
cout<<"0"; /* Used to check the byte before the function outputs it */
}
b=b*(pow(2,digits-1)); /* Turns the remainder into an appropriate value to continue the loop */
digits=digits-1;
}
cout<<"\n"<<binary <<"\n"; /* Another check i was using */
return *binary; /* This is almost certainly a major part of the problem */
delete [] binary;
}```
Peter

2. Ok, here are the main problems with your program. You allocate memory in binary_conv but you never intialize it. Then you use strcat to append data to the already garbage data. You might consider using std::string for this. If you haven't ever used it before it's pretty simple. First you include <string> at the top of your program. Then you declare a string object in the beginning of binary_conv like

Code:
`string binary;`
Then instead of using strcat to append , you can simply use += operator

Code:
`binary += "1";`
Also, you won't have to worry about cleaning up the memory after yourself. Then at the end, what you need to do is convert the ascii representation of the binary to an integer. Use the function atoi for this.

Code:
`return atoi(binary.c_str());`
c_str is a method of the string class. It returns a const char * so that you can use functions that don't take a string as a parameter.

If you this sounds too complicated for you and you want to stick with the method you had, you will need to zero out your memory buffer before strcat'ing on it.

Code:
`memset(binary, 0, digits + 1);`
Then at the end you will still need to use atoi , but you will be able to directly pass binary.

3. Wow, thanks a lot! I know it prob took some time to go through the code and i really appreciate it

4. Thanks again! It works perfectly! One minor question i have is to the action of c_str(). What exactly does it do to the string and how does it allow other functions to work with a string? Sorry, i must sound very stupid but i find that unless i understand things well i can never implement them again

5. some function onyl accept c style strings,
it just turns it into a c style stream,
being null terminated (i think that the word)

how a c string has '\0' at the end i guess that that
is the equivlant.

6. also maybe this just some kind of practice for you,
you you didnt know that this exsisted, so ill just
put it on the table just incase your interested.

Code:
```#include <bitset>
#include <iostream>
#include <string>

using namespace std;

void tobin(int);

int main()
{
for(int x = 0; x != 10; x++)
{
tobin(x);
}
cin.get();
return 0;
}

void tobin(int num)
{
bitset<32> b(num);
cout << b << endl;
}```

7. Cool! Thanks for the further info/explanation Yeah, it was kinda just an exercise in both working out the conversion and practicing writing in C++ but its useful to know this stuff anyways as i have no idea about any of it!
Hmmm, i wonder what my next project should be I mite go the other way and then group into into a menu etc. as havent ever done that Thanks again for the help both of you!!!!

8. If we're just doing binary representation, and not some other base, then you can use a "bitmask" to check if a bit at any place in an int is set.
Code:
```#include <iostream>
#include <string>

int main()
{
char toConvert = 'U';
std::string binary;