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

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    3

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

    Given For e.g,

    Code:
    char input[] = "10011210582553796";

    now Hexadecimal of 10011210582553796 is 2391249A8B74C4.

    So output unsigned char* should have value,
    Code:
    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
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You mean convert decimal integers to hex? Yes there is.
    Code:
    int myNumber = 10;
    
    printf("hex of %d is %X", myNumber, myNumber);
    And if that's not what you mean, explain what you're having trouble with. And not "give me teh codez".

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I don't suppose you read my reply here did you?
    http://forums.devshed.com/c-programm...ed-541856.html

    Or are you just gonna run from site to site in the hopes of eventually finding someone to feed you the answer without you showing any effort?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    3
    Hi zacs7,

    The number i have given is out of range of int datatype, so can not use printf of or atoi operation which are supported for int datatype. Actually number can be stored in int64 type but system does not support int64 datatype. So i want to convert is to
    Code:
    unsigned char output[8] = {0,35,145,36,154,139,116,196}  = {0,0x23,0x91,0x24,0x9A,0x8B,0x74,0xC4}
    Even i cannot use left shift or right shift (multiplication/devision) when number reaches above integer capacity.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Salem already answered your question then, http://forums.devshed.com/c-programm...ed-541856.html

    Try not to spam your question everywhere. Do you know how to convert decimal to hex in the first place?

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    3

    Thumbs up

    found solution...

    Code:
         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));
         }

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Have your tested that solution? itoa is not properly defined how have you declaried your 'a' string for the function call?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  2. Replies: 2
    Last Post: 04-21-2008, 07:43 PM
  3. Replies: 7
    Last Post: 02-08-2008, 06:31 PM
  4. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 02:13 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM