// A1Q2.cpp #include #include #include using namespace std; int main() { char compactedS[12]; int compactedV[3]={0,0,0}; int hexNumber[12]; int asc2[8]; short i=0,j=0,temp=1; cout << "Please enter the compacted string of bits in Hexadecimal format: "; cin >> compactedS; // get input compacted values as Char; for(i=0;i<3;i++) { for(j=0;j<4;j++) { if (compactedS[i*4+j]>=97 && compactedS[i*4+j]<=102) hexNumber[i*4+j] = compactedS[i*4+j] -87; else if (compactedS[i*4+j]>=65 && compactedS[i*4+j]<=70) hexNumber[i*4+j] = compactedS[i*4+j] -55; else if (compactedS[i*4+j]>=48 && compactedS[i*4+j]<=57) hexNumber[i*4+j] = compactedS[i*4+j] -48; else temp = 0; //input invalid characters, change temp to 0; compactedV[i] = compactedV[i] + hexNumber[i*4+j] * (int)pow(16,3-j); // trasfer char to integer value; } } for(i=0;i<8;i++) { asc2[i]=compactedV[i/3]-compactedV[i/3]/40*40; compactedV[i/3]=(compactedV[i/3]-asc2[i])/40; // converting compacted values back to number from 1 to 36; } for(i=0;i<8;i++) { if (asc2[i]>=27 && asc2[i]<=36) asc2[i]=asc2[i]+21; //convert '0' to '9' to ASCII code. else if (asc2[i]>=1 && asc2[i]<=26) asc2[i]=asc2[i]+64; //convert 'A' to 'Z' to ASCII code. else temp=0; } if (temp!=0) { cout << "The original user id in ASCII format is "; for(i=0;i<8;i++) cout << hex << uppercase << asc2[i] << " "; // output cout << "\n" ; cout << "(" ; for(i=0;i<8;i++) cout << (char)asc2[i]; cout << ")\n" << endl; } else cout << "You input invalid character." << endl; return 0; }