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

Discussion in 'C' started by passionpatel, Jul 6, 2008.

  1. passionpatel

    passionpatel New Member

    Joined:
    Jul 6, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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};

    can anyone give a 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. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    It's easy enough, how far have you got and where are you stuck? We won't write your program for you (unless you're willing to pay) but we'll help you debug it.
     
  3. passionpatel

    passionpatel New Member

    Joined:
    Jul 6, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Re: any solution to convert given char*(number) to required unsigned char*(hex of num

    found the solution
    Code:
         int number = 0;
         char numberchars[] = "12545555685986589";
         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));
         }
     
    Last edited by a moderator: Jul 16, 2008

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice