A
A_StClaire_
hey there,
I was able to read a char string containing only digits and convert it
to its int equivalent. however now I need to do the same thing with a
char string containing a hexadecimal number.
below is my clumsy attempt. I think I have most of the problem covered
but I am finding it somewhat difficult to convert the 'a' through 'f'
values correctly. any suggestions anyone has would be tremendously
appreciated.
thx
a newb
#include <iostream>
using namespace std;
int sixteen_power(int x);
int main() {
const int max_size = 100;
char char_array[max_size];
int int_array[max_size];
int i = 0;
int array_size = 0;
int counter = 0;
bool hexa = false;
cout << "Enter a numeric string. The maximum size is 100
characters.\n\n";
cin.getline(char_array, max_size);
if(char_array[0] == '0' && char_array[1] == 'x') {
hexa = true;
i = 2;
while(counter < strlen(char_array)) {
char_array[i - 2] = char_array;
i++;
counter++;
}
char_array[i - 1] = NULL;
char_array = NULL;
i = 0;
counter = 0;
}
array_size = strlen(char_array);
while(i < array_size) {
int_array = char_array;
int_array -= 48;
i++;
}
i = 0;
if(hexa == true) {
int sum = 0;
while(i < array_size) {
int_array = int_array *
(sixteen_power(array_size - i - 1));
i++;
}
i = 0;
while(i < array_size) {
sum += int_array;
i++;
}
int_array[0] = sum;
i = 1;
while(i < array_size) {
int_array = NULL;
i++;
}
array_size = 1;
i = 0;
}
cout << "\nThe number you entered is:\n\n";
while(i < array_size) {
cout << int_array;
i++;
}
cin.get();
return 0;
}
int sixteen_power(int x) {
int power = x;
int counter = 0;
int result = 1;
while(counter < x) {
result *= 16;
counter++;
}
return result;
}
I was able to read a char string containing only digits and convert it
to its int equivalent. however now I need to do the same thing with a
char string containing a hexadecimal number.
below is my clumsy attempt. I think I have most of the problem covered
but I am finding it somewhat difficult to convert the 'a' through 'f'
values correctly. any suggestions anyone has would be tremendously
appreciated.
thx
a newb
#include <iostream>
using namespace std;
int sixteen_power(int x);
int main() {
const int max_size = 100;
char char_array[max_size];
int int_array[max_size];
int i = 0;
int array_size = 0;
int counter = 0;
bool hexa = false;
cout << "Enter a numeric string. The maximum size is 100
characters.\n\n";
cin.getline(char_array, max_size);
if(char_array[0] == '0' && char_array[1] == 'x') {
hexa = true;
i = 2;
while(counter < strlen(char_array)) {
char_array[i - 2] = char_array;
i++;
counter++;
}
char_array[i - 1] = NULL;
char_array = NULL;
i = 0;
counter = 0;
}
array_size = strlen(char_array);
while(i < array_size) {
int_array = char_array;
int_array -= 48;
i++;
}
i = 0;
if(hexa == true) {
int sum = 0;
while(i < array_size) {
int_array = int_array *
(sixteen_power(array_size - i - 1));
i++;
}
i = 0;
while(i < array_size) {
sum += int_array;
i++;
}
int_array[0] = sum;
i = 1;
while(i < array_size) {
int_array = NULL;
i++;
}
array_size = 1;
i = 0;
}
cout << "\nThe number you entered is:\n\n";
while(i < array_size) {
cout << int_array;
i++;
}
cin.get();
return 0;
}
int sixteen_power(int x) {
int power = x;
int counter = 0;
int result = 1;
while(counter < x) {
result *= 16;
counter++;
}
return result;
}