Hex string to Dec String

Discussion in 'C++' started by PSN, Feb 9, 2010.

  1. PSN

    PSN Guest

    Hi all,
    I have tried all types of keywords but was not able to find anything
    similar to my question. Anyway, here is my problem:

    I like to convert a very big hex string (>64bits) to a decimal based
    string .. or may be a very big binary string (>64bits) to a decimal
    based string .. Can anyone please suggest how this can be achieved
    logically ???

    ex: char *pHexStr = "11223344556677889900AABBCCDDEEFF" ==> Decimal
    equivalent !!!

    Thank you all for your time,

    Regards,
    P.
    PSN, Feb 9, 2010
    #1
    1. Advertising

  2. * PSN:
    > Hi all,
    > I have tried all types of keywords but was not able to find anything
    > similar to my question. Anyway, here is my problem:
    >
    > I like to convert a very big hex string (>64bits) to a decimal based
    > string .. or may be a very big binary string (>64bits) to a decimal
    > based string .. Can anyone please suggest how this can be achieved
    > logically ???
    >
    > ex: char *pHexStr = "11223344556677889900AABBCCDDEEFF" ==> Decimal
    > equivalent !!!


    Create routines

    std::string add( std::string const& a, std::string const& b );
    std::string decimalFromHexDigit( char const digit );

    Then

    std::string times2( std::string const& decimalDigits );
    std::string times4( std::string const& decimalDigits );
    std::string times16( std::string const& decimalDigits );

    and then you can walk your hex string from left to right,

    std::string decimalFromHex( std::string const& hexDigits )
    {
    int const n = hexDigits.length();
    std::string result = "0";
    for( i = 0; i < n; ++i )
    {
    result = add(
    times16( result ), decimalFromHexDigit( hexDigits )
    );
    }
    }

    Disclaimer: off-the-cuff code.


    Cheers & hth.,

    - Alf
    Alf P. Steinbach, Feb 9, 2010
    #2
    1. Advertising

  3. PSN <> wrote:
    > Hi all,
    > I have tried all types of keywords but was not able to find anything
    > similar to my question. Anyway, here is my problem:


    > I like to convert a very big hex string (>64bits) to a decimal based
    > string .. or may be a very big binary string (>64bits) to a decimal
    > based string .. Can anyone please suggest how this can be achieved
    > logically ???


    > ex: char *pHexStr = "11223344556677889900AABBCCDDEEFF" ==> Decimal
    > equivalent !!!


    Well, that string is clearly ((16^34 - 1)/15 + 238)/255, which you could
    almost work out in your head. ;-)

    > Thank you all for your time,


    > Regards,
    > P.


    --
    Alan Mackenzie (Nuremberg, Germany).
    Alan Mackenzie, Feb 9, 2010
    #3
  4. PSN

    Jorgen Grahn Guest

    On Tue, 2010-02-09, PSN wrote:
    > Hi all,
    > I have tried all types of keywords but was not able to find anything
    > similar to my question. Anyway, here is my problem:
    >
    > I like to convert a very big hex string (>64bits) to a decimal based
    > string .. or may be a very big binary string (>64bits) to a decimal
    > based string .. Can anyone please suggest how this can be achieved
    > logically ???
    >
    > ex: char *pHexStr = "11223344556677889900AABBCCDDEEFF" ==> Decimal
    > equivalent !!!


    Why? I can imagine no need for so gigantic integers (roughly 2.3e+37
    according to Python) without doing numerics on it. In which case you
    need some bignum library, which (I assume) includes string conversion
    routines which solve your problem.

    (But of course my imagination has limits).

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Feb 9, 2010
    #4
    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. Replies:
    10
    Views:
    6,144
    Neredbojias
    Aug 19, 2005
  2. Replies:
    2
    Views:
    2,002
    Henri Sivonen
    May 28, 2005
  3. ^_^
    Replies:
    2
    Views:
    368
    Ben Pfaff
    Jan 6, 2004
  4. Bengt Richter
    Replies:
    6
    Views:
    450
    Juha Autero
    Aug 19, 2003
  5. J.E./C.Y.Cripps
    Replies:
    0
    Views:
    120
    J.E./C.Y.Cripps
    Dec 3, 2004
Loading...

Share This Page