Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
-
The problem statement, all variables and given/known data:
Split a 64bit hexadecimal number into two 32 bit numbers or keys. -
Relevant commands, code, scripts, algorithms:
double key = 0; //Stores the key value entered by the user.
double key_copy = key;
int k1; //1st half of key.
int k2; //2nd half of key.
- The attempts at a solution (include all code and scripts):
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
int main( int argc, char * argv[] )
{
double key = 0; //Stores the key value entered by the user.
double index = 15; /*Used for pow function. For loop index corresponds to
hexadecimal powers.*/
if( argc > 1 )
{
if( strlen( argv[1] ) == 16 )
{
for( int i = 0; i < 16; i++ )
{
switch( argv[1] )
{
case '0': key += 0 * pow( 16.0, index );
break;
case '1': key += 1 * pow( 16.0, index );
break;
case '2': key += 2 * pow( 16.0, index );
break;
case '3': key += 3 * pow( 16.0, index );
break;
case '4': key += 4 * pow( 16.0, index );
break;
case '5': key += 5 * pow( 16.0, index );
break;
case '6': key += 6 * pow( 16.0, index );
break;
case '7': key += 7 * pow( 16.0, index );
break;
case '8': key += 8 * pow( 16.0, index );
break;
case '9': key += 9 * pow( 16.0, index );
break;
case 'A': key += 10 * pow( 16.0, index );
break;
case 'B': key += 11 * pow( 16.0, index );
break;
case 'C': key += 12 * pow( 16.0, index );
break;
case 'D': key += 13 * pow( 16.0, index );
break;
case 'E': key += 14 * pow( 16.0, index );
break;
case 'F': key += 15 * pow( 16.0, index );
break;
default: cout << "You entered a non-hexadecimal value.\n";
exit(0);
}
index--;
}
double key_copy = key;
int k1; //1st half of key.
int k2; //2nd half of key.
key_copy /= pow( 2.0, 32.0 ); //Shift key to right to get 1st half of binary.
k1 = (int) key_copy;
key_copy = key;
key_copy *= pow( 2.0, 31.0 ); //Shift key to left then right to get 2nd half
key_copy /= pow( 2.0, 31.0 ); //of binary representation.
k2 = key_copy;
cout << k1 << endl;
cout << k2 << endl;
}
}
return 0;
}
- Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
University of Missouri Saint Louis, Saint Louis (Missouri), United States, Michael Schulte, 4780 (cs.umsl.edu/~schulte)
Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).