Hexadecimal To Decimal Conversion (Via Char Array)

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;
}
 
J

John Ratliff

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.

I would probably use the ANSI C strtol function.

----------- test.cc --------------------------------
#include <iostream>
#include <cstdlib>

int main() {
char hex[] = "1000";
int i = static_cast<int>(strtol(hex, NULL, 16));

std::cout << "i = " << i << '\n';

return 0;
}
 
A

A_StClaire_

John said:
I would probably use the ANSI C strtol function.

----------- test.cc --------------------------------
#include <iostream>
#include <cstdlib>

int main() {
char hex[] = "1000";
int i = static_cast<int>(strtol(hex, NULL, 16));

std::cout << "i = " << i << '\n';

return 0;
}
----------------------------------------------------

Output is 4096 by the way.

--John Ratliff


thx, John.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top