C/C++
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   Web Development Archives FAQs C/C++

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Web Development Archives Sponsor:
  #1  
Old July 6th, 2008, 06:53 AM
passionpatel passionpatel is offline
Registered User
Dev Archives Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 2 passionpatel User rank is Just a Lowly Private (1 - 20 Reputation Level)  
Time spent in forums: 4 m 36 sec
Reputation Power: 0
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...

Reply With Quote
  #2  
Old July 6th, 2008, 09:16 PM
JohnFrank JohnFrank is offline
Contributing User
Dev Archives Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Location: Lilyfield NSW Australia
Posts: 30 JohnFrank User rank is Just a Lowly Private (1 - 20 Reputation Level)  
Time spent in forums: 5 h 3 m 47 sec
Reputation Power: 1
Send a message via MSN to JohnFrank
Quote:
Originally Posted by passionpatel
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...
For the format conversion you can use something like sprintf, but for the data size you will have a problem unless you write a routine to convert numbers that large, also you will need to think about whether the length is fixed or variable.

Reply With Quote
  #3  
Old July 15th, 2008, 11:20 AM
passionpatel passionpatel is offline
Registered User
Dev Archives Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 2 passionpatel User rank is Just a Lowly Private (1 - 20 Reputation Level)  
Time spent in forums: 4 m 36 sec
Reputation Power: 0
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));
}

Reply With Quote
Reply

Viewing: Web Development Archives FAQs C/C++ > Any solution to convert given char*(number) to required unsigned char*(hex of number)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway