Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ converting HELP: hex to decimal but using a 64bit 16byte hex

Options
  • 12-04-2007 8:22pm
    #1
    Registered Users Posts: 43


    If this is the wrong place for this then sorry :p

    ok here is the problem

    i get an input from the user in hex format and of 16hex values ie 8bytes long(64bits) and is stored in the following:

    char hex[16];

    and then i am using the following to convert it to decimal

    sscanf( hex, "%X", &hexInt );
    itoa(hexInt,binChar,2);

    where

    uint64_t hexInt;
    char binChar[64];

    now this seems to work but i am only getting an 8bit output and i do not think its is even the right output

    any help ?


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Shouldn't itoa(hexInt,binChar,2); be itoa(hexInt,binChar,16); ?


  • Registered Users Posts: 43 nellyrev


    oh god no :p

    im converting hex to dec not hex to hex

    EDIT sorry

    sscanf( hex, "%X", &hexInt ); // Hex To Dec
    itoa(hexInt,binChar,2); // Dec To Bin

    man my hungarian notation is bad :p

    it should be this

    sscanf( hex, "%X", &decInt );
    itoa(decInt,binChar,2);


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    God i'm all confused.

    Why you using itoa (Integer to Ascii) when it is Hex to double you want to do? The 2 in the itoa is for binary.

    10 is for double so it would be

    itoa(hexInt,binChar,10)


    Am i totally mis understanding you here?


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    This works
    int hex;
    	char binChar[80];
    	
    	scanf("%x",&hex);
    	itoa(hex,binChar,10);
    	printf("%s",binChar);
    

    Not sure if this what you want - you have me all confused!

    This Reads in Hex as ASCII.
    Converts ASCII (as hex value) to Double.
    Prints out this double


  • Registered Users Posts: 43 nellyrev


    say the input is:

    0123456789abcdef // variable hex

    then sscanf( hex, "%X", &hexInt ); // converts to decimal

    where hexInt has the value 2309737967

    but it should be 81985529216486895

    is sscanf the wrong command to do it with ?


  • Advertisement
  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Oh yes i see, for large numbers a problem occurs. Take it that the int just a 32bit one so it can only accomodate so much. I amn't sure to be honest :S


  • Registered Users Posts: 43 nellyrev


    thank you for trying :)

    EDIT i would show you more code but its top secret , but not really


  • Registered Users Posts: 5,379 ✭✭✭DublinDilbert


    If your writting in C++ you can just use Cin as follows:-

    long64 input_data;
    cout << "Please enter 16 hex characters";
    cin >> hex >> input_data;


  • Registered Users Posts: 43 nellyrev


    mmm the compile does not recognise long64


  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    you don't say what compiler you use or if in c/c++
    i did this c code here, but the result is off by 1 byte..so its unreliable, probably c lib routine does it all anyway, i don't know.

    [PHP]#include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main(int argc, char *argv[])
    {
    double bignumber=0.0;
    int i,length;
    char number;

    while(--argc) {

    length = strlen(argv[1]);

    for(i=0;i<length;i++) {

    number = argv[1];

    if(number >= 'a')
    number -= 'a' - 10;
    else
    number -= '0';

    bignumber *= 16;
    bignumber += number;
    }
    printf("\n%0.f",bignumber);
    }
    }[/PHP]

    example:
    test 0123456789abcdef

    output:
    81985529216486896

    but in calculator, it gives 81985529216486895 hehe

    EDIT:i noticed that while the bignumber is the same output as calc.exe on windows, printf gives bignumber + 1

    Correction:i don't know why it 1 is added, but maybe its my computer. :p
    81985529216486895 is in fpu before move to bignumber variable, but when move to stack for printf, it then has 1+ more.:confused:

    EDIT: the 1+ added, i found out, is because the use of double, i was told that __int64 and _atoi64() for GCC (what i'm using) would work, although, it will depend on compiler too..


  • Advertisement
  • Registered Users Posts: 43 nellyrev


    I am using C++ and Borland's C++ builder 6


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    No problem, but you seem to be using very C style code, you coming from C background?


  • Registered Users Posts: 1,287 ✭✭✭joe_chicken


    nellyrev wrote:
    16bytes long(64bits)

    emmm... this might be a stupid question (please don't be too harsh)

    But does 16bytes not equal 128 bits?


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    emmm... this might be a stupid question (please don't be too harsh)

    But does 16bytes not equal 128 bits?
    You are right. It is 8 bytes that is 64bit.


  • Registered Users Posts: 1,287 ✭✭✭joe_chicken


    Just a quick compile on Dev-cpp (gcc) and I see that itoa doesn't give any compile errors, but is only converting 32 bits of the 64 bits


    Doing a little googling and there's an i64toa function, but I don't seem to have any library for that.


  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    emmm... this might be a stupid question (please don't be too harsh)

    But does 16bytes not equal 128 bits?

    yeah, the string is 128-bits, but the actual value can be stored in 64-bits :)

    nellyrev, the code i wrote, if you change the format %0.f to %i64d and change double to int64 (try prefixing _) then it should work, i know it works in MSVC, but as before, probably easier way, like with atoi64

    the reason the double adds 1 more, is because the FPU is inprecise when storing the value from the stack into variable, you won't have this problem if using int64


  • Registered Users Posts: 1,287 ✭✭✭joe_chicken


    yeah, the string is 128-bits, but the actual value can be stored in 64-bits :)


    ahhh, I see... thanks


  • Registered Users Posts: 43 nellyrev


    doh on the 16byte thing

    i guess i meant 16 hex values

    anyway i got an update

    i just separated the hex into 2 which is kinda what i have to do anyway

    but still the sscanf is not giving me what i need

    here is the code :
    uint64_t hexkeyIntL = 0;
            // sets the left side of the key
            for(i=0; i<8; i++)
            hexkeyL[i] = hexkey16[i];
    
            // input hexkey16 is 0123456701234567 where hexkeyL is 01234567 & where hexkeyR is 01234567
       *     sscanf( hexkeyL, "%x", &deckeyIntL ); // gives the right result where 19088743
            _i64toa(hexkeyIntL,binkeyCharL,2);
            printf("%s",binkeyCharL);
            //sets the right side of the key
            i=0;
            uint64_t hexkeyIntR = 0;
            for(j=8;j<16;j++){
            hexkeyR[i] = hexkey16[j];
            i++;
            }
    
        *    sscanf( hexkeyR, "%x", &hexkeyIntR ); // doesnt give the right result 4294967295 meaning sscanf thinks hexkeyR is 0xFFFFFFFF
            _i64toa(hexkeyIntR,binkeyCharR,2);
            printf("%s\n",binkeyCharR);
    

    so any answers to the lines marked with *

    is there a way to convert the hex to dec or even convert the hex to bin instantly :p


  • Registered Users Posts: 43 nellyrev


    Webmonkey wrote:
    No problem, but you seem to be using very C style code, you coming from C background?

    last year in college we used C i dont really know how to do the conversion with C++.

    also Webmonkey your code is weird(over my head) to me lol


  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    using DublinDilbert's suggestion

    [PHP]
    #include <iostream>

    using namespace std;

    void toBin64(__int64 number);

    int main(int argc,char *argv[])
    {
    __int64 input_data;

    cout << "Please enter 16 hex characters:";
    cin >> hex >> input_data;

    cout << "\n\nDecimal:" << dec << input_data << endl;
    cout << "Hexadecimal:" << hex << input_data << endl;
    cout << "Binary:";
    tobin64(input_data);

    return 0;
    }

    void toBin64(__int64 number)
    {
    for(int i=63;i>=0;i--)
    cout << ((number >> i) & 1);
    }[/PHP]
    Please enter 16 hex characters:0123456789abcdef
    
    
    Decimal:81985529216486895
    Hexadecimal:123456789abcdef
    Binary:0000000100100011010001010110011110001001101010111100110111101111
    


  • Advertisement
  • Registered Users Posts: 43 nellyrev


    that is very cool and i have seen most of the above but how can i need to be able to store these conversions :) not just display them


Advertisement