Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 213263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:15:44+00:00 2026-05-11T18:15:44+00:00

The program requires an input of an arbitrary large unsigned integer which is expressed

  • 0

The program requires an input of an arbitrary large unsigned integer which is expressed as one string in base 10. The outputs is another string that expresses the integer in base 16.

For example, the input is “1234567890987654321234567890987654321234567890987654321”,
and the output shall be “CE3B5A137DD015278E09864703E4FF9952FF6B62C1CB1”

The faster the algorithm the better.

It will be very easy if the input is limited within 32-bit or 64-bit integer; for example, the following code can do the conversion:

#define MAX_BUFFER 16
char hex[] = "0123456789ABCDEF";

char* dec2hex(unsigned input) {
    char buff[MAX_BUFFER];
    int i = 0, j = 0;
    char* output;

    if (input == 0) {
        buff[0] = hex[0];
        i = 1;
    } else {
        while (input) {
            buff[i++] = hex[input % 16];
            input = input / 16;
        }
    }

    output = malloc((i + 1) * sizeof(char));
    if (!output) 
        return NULL;

    while (i > 0) {
        output[j++] = buff[--i];        
    }
    output[j] = '\0';

    return output;
}

The real challenging part is the “arbitrary large” unsigned integer. I have googled but most of them are talking about the conversion within 32-bit or 64-bit. No luck is found.

Can anyone give any hit or any link that can be read on?

Thanks in advance.

Edit This is an interview question I encountered recently. Can anyone briefly explain how to solve this problem? I know there is a gmp library and I utilized it before; however as an interview question it requires not using external library.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:15:44+00:00Added an answer on May 11, 2026 at 6:15 pm
    1. Allocate an array of integers, number of elements is equal to the length of the input string. Initialize the array to all 0s.

      This array of integers will store values in base 16.

    2. Add the decimal digits from the input string to the end of the array. Mulitply existing values by 10 add carryover, store new value in array, new carryover value is newvalue div 16.

      carryover = digit;
      for (i = (nElements-1); i >= 0; i--)
      {
          newVal = array[index] * 10) + carryover;
          array[index] = newval % 16;
          carryover = newval / 16;
      }
      
    3. print array, start at 0th entry and skip leading 0s.


    Here’s some code that will work. No doubt there are probably a few optimizations that could be made. But this should suffice as a quick and dirty solution:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include "sys/types.h"
    
    char HexChar [16] = { '0', '1', '2', '3', '4', '5', '6', '7',
                          '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    
    static int * initHexArray (char * pDecStr, int * pnElements);
    
    static void addDecValue (int * pMyArray, int nElements, int value);
    static void printHexArray (int * pHexArray, int nElements);
    
    static void
    addDecValue (int * pHexArray, int nElements, int value)
    {
        int carryover = value;
        int tmp = 0;
        int i;
    
        /* start at the bottom of the array and work towards the top
         *
         * multiply the existing array value by 10, then add new value.
         * carry over remainder as you work back towards the top of the array
         */
        for (i = (nElements-1); (i >= 0); i--)
        {
            tmp = (pHexArray[i] * 10) + carryover;
            pHexArray[i] = tmp % 16;
            carryover = tmp / 16;
        }
    }
    
    static int *
    initHexArray (char * pDecStr, int * pnElements)
    {
        int * pArray = NULL;
        int lenDecStr = strlen (pDecStr);
        int i;
    
        /* allocate an array of integer values to store intermediate results
         * only need as many as the input string as going from base 10 to
         * base 16 will never result in a larger number of digits, but for values
         * less than "16" will use the same number
         */
    
        pArray = (int *) calloc (lenDecStr,  sizeof (int));
    
        for (i = 0; i < lenDecStr; i++)
        {
            addDecValue (pArray, lenDecStr, pDecStr[i] - '0');
        }
    
        *pnElements = lenDecStr;
    
        return (pArray);
    }
    
    static void
    printHexArray (int * pHexArray, int nElements)
    {
        int start = 0;
        int i;
    
        /* skip all the leading 0s */
        while ((pHexArray[start] == 0) && (start < (nElements-1)))
        {
            start++;
        }
    
        for (i = start; i < nElements; i++)
        {
            printf ("%c", HexChar[pHexArray[i]]);
        }
    
        printf ("\n");
    }
    
    int
    main (int argc, char * argv[])
    {
        int i;
        int * pMyArray = NULL;
        int nElements;
    
        if (argc < 2)
        {
            printf ("Usage: %s decimalString\n", argv[0]);
            return (-1);
        }
    
        pMyArray = initHexArray (argv[1], &nElements);
    
        printHexArray (pMyArray, nElements);
    
        if (pMyArray != NULL)
            free (pMyArray);
    
        return (0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 115k
  • Answers 115k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try something like this in the parent: <set name="childs" inverse="true"… May 11, 2026 at 10:26 pm
  • Editorial Team
    Editorial Team added an answer It is a "Task List" plugin working. It does this:… May 11, 2026 at 10:26 pm
  • Editorial Team
    Editorial Team added an answer To elaborate on Rashmi Pandit; A ListBox with override DrawItem… May 11, 2026 at 10:26 pm

Related Questions

My program, alas, has a memory leak somewhere, but I'll be damned if I
I am trying to write a Factory Pattern to create either a MainMode or
I have a worker thread that is listening to a TCP socket for incoming
I'm looking to develop an offline version of an application that still needs to
I have an application that needs to run both on WinXP and Vista64. My

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.