The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 13 of 13

Thread: any solution to convert given char*(number) to required unsigned char*(hex of number)

  1. #1

    any solution to convert given char*(number) to required unsigned char*(hex of number)

    Given For e.g,
    char input[] = "10011210582553796";

    now Hexadecimal of 10011210582553796 is 2391249A8B74C4.

    So output unsigned char* should have value,
    unsigned char output[8] = {0x00,0x23,0x91,0x24,0x9A,0x8B,0x74,0xC4};

    Any solution to convert given input char* to output char* with above mentioned requirement.

    code should run on platform for which __int64/signed long long is not supported.

    Thanx in advance...

  2. #2
    Oh that's a bugger. I don't think the standard C library could do character manip for integers. Maybe some external library could do it. Try writing a procedure yourself.
    If you notice this notice you will notice that this notice is not worth noticing.

    "Are you doing anything tonight? If not, how about me?"

    Opera Sucks! - FIX IT
    Oliver Zheng

  3. #3
    i'd say make a function to convert the char* to a double (maybe a long double if double isnt big enough for you and you can support it) with double atof( const char *string ); then do the math to convert to hex while putting it back into your output char*[] (you can google how to convert decimal to hex)

  4. #4
    Quote Originally Posted by yaim0310 View Post
    i'd say make a function to convert the char* to a double (maybe a long double if double isnt big enough for you and you can support it) with double atof( const char *string ); then do the math to convert to hex while putting it back into your output char*[] (you can google how to convert decimal to hex)
    For the example passionpatel gave, double would be insufficiently small to hold a value that big. There needs to be character manipulation for arbitrarily large values.

    I remember I wrote a large number factorial calculator based on character manipulation. It's similar.
    If you notice this notice you will notice that this notice is not worth noticing.

    "Are you doing anything tonight? If not, how about me?"

    Opera Sucks! - FIX IT
    Oliver Zheng

  5. #5
    It might be helpful to look at how some high-level languages do it. For example, just quickly glancing through the Python source, it looks like _PyInt_Format does the work in intobject.c (follow the link to the file, then search for that method).


    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

  6. #6
    well if you #include <stdint.h> you should be able to use uint64_t for multi-platform. thats the reason behind not using __int64 right?

  7. #7
    What if I want to convert 38857383823938548393483784389485893943843848348394 38483493843843 to hex?
    If you notice this notice you will notice that this notice is not worth noticing.

    "Are you doing anything tonight? If not, how about me?"

    Opera Sucks! - FIX IT
    Oliver Zheng

  8. #8
    You shouldnt let such big numbers run rapid in your programs.

  9. #9

    found solution

    int number = 0;
    char numberchars[] = "10011210582553796";
    int i = 0;
    int ans = 0;
    int carry = 0;

    char answerArray[100] = {0};

    char remainderArray[100] = {0};
    int remindex = 0;
    int ansindex = 0;
    int remainder = 0;

    while( numberchars[i] != '\0' )
    {
    while( numberchars[i] != '\0' )
    {
    char currentchar[2] = {0};
    currentchar[0]=numberchars[i];

    int num = atoi(currentchar);
    num += remainder;
    remainder = 0;

    if ( num < 2 )
    {
    remainder = num;
    if(i>0)
    answerArray[ansindex++] = '0';
    }
    else
    {
    remainder = num % 2;
    int answer = num / 2;

    char a[2] = {0};
    itoa(answer,a,10);

    answerArray[ansindex++] = a[0];
    }

    i++;
    remainder *= 10;
    }

    char a[2] = {0};
    int rval = remainder / 10;
    itoa(remainder / 10,a,10);

    remainderArray[remindex++] = a[0];
    int size = sizeof(answerArray);
    memcpy(numberchars,answerArray,sizeof(answerArray) );
    size = sizeof(answerArray);
    memset(answerArray,0,sizeof(answerArray));
    ansindex = 0;
    remainder = 0;
    i=0;
    }

    char int64[8] = {0};

    for(int k=0;remainderArray[k]!= '\0';k++)
    {
    int64[7-(k/8)] |= ((remainderArray[k]-'0') << (k%8));
    }



    Quote Originally Posted by passionpatel View Post
    Given For e.g,
    char input[] = "10011210582553796";

    now Hexadecimal of 10011210582553796 is 2391249A8B74C4.

    So output unsigned char* should have value,
    unsigned char output[8] = {0x00,0x23,0x91,0x24,0x9A,0x8B,0x74,0xC4};

    Any solution to convert given input char* to output char* with above mentioned requirement.

    code should run on platform for which __int64/signed long long is not supported.

    Thanx in advance...

  10. #10

    Quote Originally Posted by passionpatel View Post
    found solution...
    *sigh*...

  11. #11

    Quote Originally Posted by yaim0310 View Post
    You shouldnt let such big numbers run rapid in your programs.
    That's a retarded thing to say.

    What do you mean "run rapid"? And why are big numbers such an issue? I look forward to another inanely uneducated and idiotic response.

  12. #12
    1,839
    posts
    Registered User
    That's a retarded thing to say.

    What do you mean "run rapid"? And why are big numbers such an issue? I look forward to another inanely uneducated and idiotic response.
    because theres no built in data type that can hold a number that big...afaik

    is sprintf available in C? if so thats the way to go
    char buffer[50];//to hold new string data
    int decToHex = 15;
    sprintf(buffer,"%x",decToHex);
    cout<<buffer; //prints 0xf; (or maybe just f)
    sprintf(buffer,"%X",decToHex);//note caps
    cout<<buffer; //prints 0xF; (or maybe just F)//note caps


    edit: errr or probably even beter...unless you WANT TO STORE it in a variable
    printf("%x\n",15);//prints f
    but if you just want to print it thats prolly the way to go

    edit x2: Doh someone bumped an old thread nm
    Last edited by joran420; December 20th, 2008 at 01:48 AM.

  13. #13
    You have that 15 hardcoded now. Wait till you get user imput. Then, overflow. Good luck w/ that.
    If you notice this notice you will notice that this notice is not worth noticing.

    "Are you doing anything tonight? If not, how about me?"

    Opera Sucks! - FIX IT
    Oliver Zheng

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012