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.

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

1. 2
posts
Registered User

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

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.

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. Originally Posted by yaim0310
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.

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

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. What if I want to convert 38857383823938548393483784389485893943843848348394 38483493843843 to hex?

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

9. 2
posts
Registered User
found solution

int number = 0;
char numberchars[] = "10011210582553796";
int i = 0;
int ans = 0;
int carry = 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)
}
else
{
remainder = num % 2;
int answer = num / 2;

char a[2] = {0};

}

i++;
remainder *= 10;
}

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

remainderArray[remindex++] = a[0];
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));
}

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.

10. 158
posts
Code Monkey
Originally Posted by passionpatel
found solution...
*sigh*...

11. 158
posts
Code Monkey
Originally Posted by yaim0310
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. 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. You have that 15 hardcoded now. Wait till you get user imput. Then, overflow. Good luck w/ that.