Total members 9952 | Gratitudes |It is currently Sat Feb 11, 2012 1:47 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 5 posts ] 
Author Question
 Question subject: convert char*(number) to required unsigned char*
PostPosted: Sun Jul 06, 2008 1:47 pm 
Offline
Newbie
User avatar

Joined: Sun Jul 06, 2008 1:43 pm
Posts: 3
Has thanked: 0 time
Have thanks: 0 time

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...


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: convert char*(number) to required unsigned char*
PostPosted: Mon Jul 07, 2008 5:09 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 57 time
To convert from the decimal number system into hex number system .A way to do that , first convert the number you binary and from binary take every 4 digits and make 1 digit hex ,

example : 12 )decimal to 01010)binary to C ) hex

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: convert char*(number) to required unsigned char*
PostPosted: Tue Jul 08, 2008 10:09 am 
Offline
Newbie
User avatar

Joined: Sun Jul 06, 2008 1:43 pm
Posts: 3
Has thanked: 0 time
Have thanks: 0 time
The number i have given is out of range of int datatype, so can not use printf 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 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.


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: convert char*(number) to required unsigned char*
PostPosted: Tue Jul 15, 2008 5:14 pm 
Offline
Newbie
User avatar

Joined: Sun Jul 06, 2008 1:43 pm
Posts: 3
Has thanked: 0 time
Have thanks: 0 time
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));
     }


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: convert char*(number) to required unsigned char*
PostPosted: Tue Jul 15, 2008 10:08 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 57 time
thank you so much for posting the solution :gOOd: :gOOd:

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 5 posts ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 increase the number of bank lines by <br/>  HTML examples  msi_333  0
 convert word, Excel to PDF  General Discussion  AskBot  10
 validate international phone number value  JavaScript examples  msi_333  0
 Factor of number using C++ code  C-C++  msi_333  4
 Convert RAW file system to FAT file system  General Discussion  saics  3

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 2 guests



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team