# Thread: Not sure what to call this

1. ## Not sure what to call this

How would I divide something like this by 2. Here is the code you'll need to see.

Code:
```char message[100];
cin.getline(message, 99);
for(int x=0; x<=strlen(message)-1; x++)
{
//...
}```
Here is my question... I want to make char message a fake integer...which i can do by doing.. (int)message[x];

My main question is How would I divide the value of (int)message[x] by 2...not the value of the char but the ASCI value.

I've tried (int)message[x]/=2; and (int)message[x]=/2;
and neither of those worked, even though i knew the second one wouldn't work. Yea..how would i do that?

Edit::I don't know if it makes a difference...but I also want to return the value (int)message[x] to (int)message...although it isn't necessary. But how else would i do it, make the value of an array element in terms of ASCI equal to an integer? <---How?

2. int half = (int)message[x] / 2;

-Prelude

3. ## ..the code...what's wrong

Ok, I've been sick these past few days...so I'm really not thinking right, and this is annoying me to much. So, yes...what's wrong with this...It's not giving a correct binary number...

Code:
```#include <iostream>
using namespace std;
main()
{
char message[100];
int half;
cout<<"Enter a message to convert into Binary" <<endl;
cin.getline(message, 99);
for(int x=0; x<=strlen(message)-1; x++)
{
if((int)message[x]>=128) cout<<"1";
else cout<<"0";
half = (int)message[x] / 2;
if(half>=64) cout<<"1";
else cout<<"0";
half = (int)message[x] / 2;
if(half>=32) cout<<"1";
else cout<<"0";
half = (int)message[x] / 2;
if(half>=16) cout<<"1";
else cout<<"0";
half = (int)message[x] / 2;
if(half>=8) cout<<"1";
else cout<<"0";
half = (int)message[x] / 2;
if(half>=4) cout<<"1";
else cout<<"0";
half = (int)message[x] / 2;
if(half>=2) cout<<"1";
else cout<<"0";
half = (int)message[x] / 2;
if(half>=1) cout<<"1 ";
else cout<<"0 ";
}
cout<<endl;
return 0;
}```
Edit::The problem isn't in the (int)message[x] either...caz that does show the correct ASCI #...it's all that binary stuff that's screwing up.
Edit2::I think the problem is in that half is not storing the values...as in...in the last if statement....half is the same value as the first if 2nd if statement...but how do i fix that...

4. try sliding all of the ascii bits to the right, then typecasting the result as an integer. you will always lose the LSD, but it will only matter if it's a 1

5. also, try setting up your half variable as an array with the same value as x in your character array

Code:
`half[x] = (int)message[x]   // etc...`

Popular pages Recent additions