Velocity Reviews > C++ > Hexadecimal To Decimal Conversion (Via Char Array)

# Hexadecimal To Decimal Conversion (Via Char Array)

A_StClaire_@hotmail.com
Guest
Posts: n/a

 08-12-2005

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];
i++;
counter++;
}

char_array[i - 1] = NULL;
char_array[i] = NULL;
i = 0;
counter = 0;
}

array_size = strlen(char_array);

while(i < array_size) {
int_array[i] = char_array[i];
int_array[i] -= 48;
i++;
}

i = 0;

if(hexa == true) {
int sum = 0;

while(i < array_size) {
int_array[i] = int_array[i] *
(sixteen_power(array_size - i - 1));
i++;
}
i = 0;

while(i < array_size) {
sum += int_array[i];
i++;
}

int_array[0] = sum;
i = 1;

while(i < array_size) {
int_array[i] = NULL;
i++;
}

array_size = 1;
i = 0;
}

cout << "\nThe number you entered is:\n\n";

while(i < array_size) {
cout << int_array[i];
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;
}

John Ratliff
Guest
Posts: n/a

 08-12-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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;
}
----------------------------------------------------

Output is 4096 by the way.

--John Ratliff

A_StClaire_@hotmail.com
Guest
Posts: n/a

 08-13-2005

John Ratliff wrote:

> 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.