TomHardy - 3 years ago 166
C++ Question

# Q: Single character to array

I am trying to come up with a code... let me explain...

1. I will use input to write a HEX number (A5B)

2. I will put the HEX number, each of its character to one location of array X (X[0]=A,X[1]=5,X[2]=B).

3. Then I want to use a SWITCH statement to find the coresponding BIT number for each of its HEX brother:

``````switch(x[i])
{
case '0': cout << "0000"; break;
case '1' :cout << "0001"; break;
case '2': cout << "0010"; break;
case '3': cout << "0011"; break;
case '4': cout << "0100"; break;
case '5': cout << "0101"; break;
case '6': cout << "0110"; break;
case '7': cout << "0111"; break;
case '8': cout << "1000"; break;
case '9': cout << "1001"; break;
case 'A': cout << "1010"; break;
case 'B': cout << "1011"; break;
case 'C': cout << "1100"; break;
case 'D': cout << "1101"; break;
case 'E': cout << "1110"; break;
case 'F': cout << "1111"; break;
}
``````

4. each of it i want to put into array Z, like for example (Z[0]=1010,Z[1]=0101,Z[2]=1011);

5. and then finaly print the array Z in reverse but to one single INT Bin.

Can you guys help me with that? I dont know how to do the input single character to Array, or the output of the array to single INT.
The output of the array to single bit could be something like this?

``````while (t!=0){
int Bin=Bin + Z[t];
Bin=Bin+g;
g*=1000;
t-=1;
}
``````

the "t" is the size of the array z from the step number 4. it will write the volume of that location to int Bin and then multiply the int Bin by 1 000, (for the shifting of the bin by four to the left) I dont know if you understand me
English is not my native language but you get the idea from the code (that is magic because when someone in englis writes the code and sent it to Germany and they can't speak englis they can understand him)... okey but back to the topic... can you help me please? Thank you.

I don't exactly understand your question but from what I could gather this appears to be what you want. using namespace std;

int main(){

``````string x;
cin>>x;
string z[x.size()];
for (int i = 0; i < x.size(); ++i){
switch(x[i]){
case '0': z[i] = "0000";
break;
case '1' :z[i] = "0001";break;
case '2': z[i] = "0010";break;
case '3': z[i] = "0011";break;
case '4': z[i] = "0100";break;
case '5': z[i] = "0101";break;
case '6': z[i] = "0110";break;
case '7': z[i] = "0111";break;
case '8': z[i] = "1000";break;
case '9': z[i] = "1001";break;
case 'A': z[i] = "1010";break;
case 'B': z[i] = "1011";break;
case 'C': z[i] = "1100";break;
case 'D': z[i] = "1101";break;
case 'E': z[i] = "1110";break;
case 'F': z[i] = "1111";break;
}
}
string s(4*x.size(),'0');
int k = 0;
for (int i = x.size()-1; i >= 0; i--){
for (int j = 3; j >= 0; j--){
s[k] = z[i][j];
k++;
}
}
cout << s << endl;
``````

} Also include iostream at the start. PS: I still don't understand how to write as a code in this website properly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download