# Any solution to convert given char\*(number) to required unsigned char\*(hex of number)

**URL:** <https://forum.kirupa.com/t/any-solution-to-convert-given-char-number-to-required-unsigned-char-hex-of-number/265215>\
**Category:** Uncategorized\
**Created:** [July 6, 2008, 1:08pm UTC](https://forum.kirupa.com/t/any-solution-to-convert-given-char-number-to-required-unsigned-char-hex-of-number/265215 "2008-07-06T13:08:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![passionpatel](https://avatars.discourse-cdn.com/v4/letter/p/cdc98d/32.png) [@passionpatel](https://forum.kirupa.com/u/passionpatel)\
**Post date:** [July 6, 2008, 1:08pm UTC](https://forum.kirupa.com/t/any-solution-to-convert-given-char-number-to-required-unsigned-char-hex-of-number/265215/1 "2008-07-06T13:08:42Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![MTsoul](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@MTsoul](https://forum.kirupa.com/u/MTsoul)\
**Post date:** [July 6, 2008, 4:50pm UTC](https://forum.kirupa.com/t/any-solution-to-convert-given-char-number-to-required-unsigned-char-hex-of-number/265215/2 "2008-07-06T16:50:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yaim0310](https://avatars.discourse-cdn.com/v4/letter/y/85e7bf/32.png) [@yaim0310](https://forum.kirupa.com/u/yaim0310)\
**Post date:** [July 6, 2008, 9:25pm UTC](https://forum.kirupa.com/t/any-solution-to-convert-given-char-number-to-required-unsigned-char-hex-of-number/265215/3 "2008-07-06T21:25:59Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![MTsoul](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@MTsoul](https://forum.kirupa.com/u/MTsoul)\
**Post date:** [July 6, 2008, 10:01pm UTC](https://forum.kirupa.com/t/any-solution-to-convert-given-char-number-to-required-unsigned-char-hex-of-number/265215/4 "2008-07-06T22:01:09Z")

</div>

[QUOTE=yaim0310;2354337]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)[/QUOTE]  
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.

---

<div class="post-metadata">

**Author:** ![Jeff\_Wheeler](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jeff\_Wheeler](https://forum.kirupa.com/u/Jeff_Wheeler)\
**Post date:** [July 6, 2008, 10:42pm UTC](https://forum.kirupa.com/t/any-solution-to-convert-given-char-number-to-required-unsigned-char-hex-of-number/265215/5 "2008-07-06T22:42:18Z")

</div>

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 [font=monospace] \_PyInt\_Format[/font] does the work in [intobject.c](http://svn.python.org/view/python/trunk/Objects/intobject.c?rev=64753&view=markup) (follow the link to the file, then search for that method).

---

<div class="post-metadata">

**Author:** ![yaim0310](https://avatars.discourse-cdn.com/v4/letter/y/85e7bf/32.png) [@yaim0310](https://forum.kirupa.com/u/yaim0310)\
**Post date:** [July 7, 2008, 12:41am UTC](https://forum.kirupa.com/t/any-solution-to-convert-given-char-number-to-required-unsigned-char-hex-of-number/265215/6 "2008-07-07T00:41:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![MTsoul](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@MTsoul](https://forum.kirupa.com/u/MTsoul)\
**Post date:** [July 7, 2008, 1:06am UTC](https://forum.kirupa.com/t/any-solution-to-convert-given-char-number-to-required-unsigned-char-hex-of-number/265215/7 "2008-07-07T01:06:42Z")

</div>

What if I want to convert 3885738382393854839348378438948589394384384834839438483493843843 to hex?

---

<div class="post-metadata">

**Author:** ![yaim0310](https://avatars.discourse-cdn.com/v4/letter/y/85e7bf/32.png) [@yaim0310](https://forum.kirupa.com/u/yaim0310)\
**Post date:** [July 7, 2008, 2:03am UTC](https://forum.kirupa.com/t/any-solution-to-convert-given-char-number-to-required-unsigned-char-hex-of-number/265215/8 "2008-07-07T02:03:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![passionpatel](https://avatars.discourse-cdn.com/v4/letter/p/cdc98d/32.png) [@passionpatel](https://forum.kirupa.com/u/passionpatel)\
**Post date:** [July 15, 2008, 4:38pm UTC](https://forum.kirupa.com/t/any-solution-to-convert-given-char-number-to-required-unsigned-char-hex-of-number/265215/9 "2008-07-15T16:38:01Z")

</div>

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* != '\0' )
 {
      while( numberchars* != '\0' )
      {
           char currentchar[2] = {0};
           currentchar[0]=numberchars*;

           int num = atoi(currentchar);
           num += remainder;
           remainder = 0;

           if ( num &lt; 2 )
           {               
                remainder = num;
                if(i&gt;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') &lt;&lt; (k%8));
 }

```

[quote=passionpatel;2354194]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…[/quote]
