如何将任意大整数转换基地10基地16? [英] How to convert an arbitrary large integer from base 10 to base 16?

查看:184
本文介绍了如何将任意大整数转换基地10基地16?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序需要一个任意大的无符号整数,这是基地10的输出pssed作为一个字符串的前$ P $的输入是另一个字符串前presses在基地16整数。

例如,在输入是1234567890987654321234567890987654321234567890987654321,
和输出应为CE3B5A137DD015278E09864703E4FF9952FF6B62C1CB1

越快算法越好。

这将是很容易的,如果输入中的32位或64位整数限制;例如,下面的code可以执行转换:

 的#define MAX_BUFFER 16
十六进制字符[] =0123456789ABCDEF;字符* DEC2HEX(无符号输入){
    字符的buff [MAX_BUFFER]
    INT I = 0,J = 0;
    字符*输出;    如果(输入== 0){
        抛光轮[0] =进制[0];
        I = 1;
    }其他{
        而(输入){
            BUFF [我++] =十六进制[输入%16];
            输入=输入/ 16;
        }
    }    输出=的malloc(第(i + 1)* sizeof的(炭));
    如果(!输出)
        返回NULL;    而(ⅰ大于0){
        输出[J ++] = BUFF [ - 我]
    }
    输出[J] ='\\ 0';    返回输出;
}

真正具有挑战性的部分是随意性大无符号整数。我用Google搜索,但他们大多都在谈论在32位或64位的转换。没有运气找到。

谁能给任何命中还是可以读取上的任何链接?

先谢谢了。

修改这是我最近遇到的面试问题。任何人都可以简单介绍一下如何解决这个问题?我知道有一个GMP图书馆和我之前使用它;然而,作为一个面试问题它需要不使用外部库。


解决方案

  1. 分配整数数组,元件的数目等于输入字符串的长度。数组初始化为全0。

    整数这个数组将存储在基地16个值。


  2. 从输入字符串数组的末尾添加小数位数。 Mulitply 10加结转现有的价值,存放在数组新的价值,新的残留值是NEWVALUE DIV 16。

      =结转数字;
    为(ⅰ=(nElements-1); I> = 0;我 - )
    {
        的newval =数组[索引] * 10)+结转;
        数组[索引] =的newval%16;
        结转=的newval / 16;
    }


  3. 印刷阵列,开始于第0个条目,并跳过前导零。



下面是一些code,将工作。毫无疑问,有可能是可以作出一些优化。但这应该足以作为一个快速和肮脏的解决方案:

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;
#包括SYS / types.h中炭HexChar [16] = {'0','1','2','3','4','5','6','7',
                      '8','9','A','B','C','D','E','F'};静态INT * initHexArray(字符* pDecStr,为int * pnElements);静态无效addDecValue为(int * pMyArray,诠释nElements,int值);
静态无效printHexArray为(int * pHexArray,诠释nElements);静态无效
addDecValue为(int * pHexArray,诠释nElements,int值)
{
    INT结转=价值;
    INT TMP = 0;
    INT I;    / *开始在阵列的底部并朝向顶部工作
     *
     *乘以10的现有阵列值,然后添加新的价值。
     *结转余,你对阵列的顶部下班回来
     * /
    为(ⅰ=(nElements-1);(I&GT = 0);我 - )
    {
        TMP =(pHexArray [I] * 10)+结转;
        pHexArray [I] = TMP%16;
        结转= TMP / 16;
    }
}静态INT *
initHexArray(字符* pDecStr,为int * pnElements)
{
    为int *粒子阵列= NULL;
    INT lenDecStr = strlen的(pDecStr);
    INT I;    / *分配整数数组存储中间结果
     *只需要一样多的输入字符串为从基座10要
     *底座16绝不会导致数字的数量较多,但对值
     *小于16,将使用相同​​数量的
     * /    粒子阵列=(INT *)释放calloc(lenDecStr,sizeof的(INT));    对于(i = 0; I< lenDecStr;我++)
    {
        addDecValue(粒子阵列,lenDecStr,pDecStr [I] - '0');
    }    * pnElements = lenDecStr;    返回(粒子阵列);
}静态无效
printHexArray为(int * pHexArray,诠释nElements)
{
    INT开始= 0;
    INT I;    / *跳过所有前导零* /
    而((pHexArray [开始] == 0)及及(开始≤(nElements-1)))
    {
        启动++;
    }    对于(i =启动; I< nElements;我++)
    {
        的printf(%C,HexChar [pHexArray [我]]);
    }    的printf(\\ n);
}INT
主(INT ARGC,CHAR *的argv [])
{
    INT I;
    为int * pMyArray = NULL;
    INT nElements;    如果(的argc 2)
    {
        的printf(用法:%s的decimalString \\ n,argv的[0]);
        返回(-1);
    }    pMyArray = initHexArray(的argv [1],&放大器; nElements);    printHexArray(pMyArray,nElements);    如果(pMyArray!= NULL)
        免费(pMyArray);    返回(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. 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);
}

这篇关于如何将任意大整数转换基地10基地16?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆