Hexadecimal To Decimal Conversion (Via Char Array)

Discussion in 'C++' started by A_StClaire_@hotmail.com, Aug 12, 2005.

  1. Guest

    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;
    }
     
    , Aug 12, 2005
    #1
    1. Advertising

  2. John Ratliff Guest

    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
     
    John Ratliff, Aug 12, 2005
    #2
    1. Advertising

  3. Guest

    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.
     
    , Aug 13, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sara

    decimal to hexadecimal

    sara, Dec 28, 2004, in forum: C Programming
    Replies:
    11
    Views:
    1,245
    Dietmar Schindler
    Jan 5, 2005
  2. muss
    Replies:
    13
    Views:
    723
    Vladimir S. Oka
    Mar 27, 2006
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,096
    Ian Collins
    May 9, 2006
  4. Replies:
    14
    Views:
    1,053
    Simon Biber
    Nov 16, 2006
  5. Ven
    Replies:
    3
    Views:
    1,339
Loading...

Share This Page