• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert Whole numbers to Hex
#1
This code simply takes in a number from command line input (compiled.exe 12345) and converts to hex, doesn't work with decimals, but could easily be modified too, you would need to use an IF statement instead of a case switch.

[code=cpp]#include <iostream>
using namespace std;
#include <math.h>
#include <stdlib.h>


main(int argc, char *argv[]){
int input = atoi(argv[1]);
int len = ceil(log10(input));
int dec[len];
float remainder;
float floa = input;
int x=len;
float fullnumber;

for(;x >= 0; x--){
remainder = floa/16;
cout << floa << " / 16 = " << remainder;
floa = (int)remainder;
remainder = (remainder-(int)remainder)*16; //remainder should be in hex
cout << remainder << endl;
dec[x] = remainder;
}

x = 1;

for(;x <= len; x++){
//cout << (int)dec[x] << endl;
switch( (int)dec[x] ){
case 10:
cout << "a";
break;
case 11:
cout << "b";
break;
case 12:
cout << "c";
break;
case 13:
cout << "d";
break;
case 14:
cout << "e";
break;
case 15:
cout << "f";
break;
default:
cout << dec[x];
break;
}
}
return 0;
}[/code]
[Image: nomnomnom.jpg]
;7$=v?%v%#5>v7v8994
The decrypt code is V, I could not make it any simpler!
  Reply


Messages In This Thread
Convert Whole numbers to Hex - by Drumm - 03-01-2011, 01:29 PM

Forum Jump: