MDGSF Software Engineer

任意不同的进制转换

2016-03-30
Art

void vReverse(char * pc)
{
    char acTemp;
    int iLen = strlen(pc);
    for (int i=0; i<=(iLen-2)/2; i++)
    {
        acTemp = pc[i];
        pc[i] = pc[iLen-1-i];
        pc[iLen-1-i]=acTemp;
    }
}

void vN2N(const char * pcSource, char * pcDest, int iBase1, int iBase2)
{
    const char * pcHead = &pcSource[0];
    int iSum = 0;
    int iIndex;
    for (iIndex=0; pcHead[iIndex] != '\0'; iIndex++)
    {
        iSum *= iBase1;
        if(pcHead[iIndex]<='9' && pcHead[iIndex]>='0')
        {
            iSum = iSum + (pcHead[iIndex] - '0');
        }
        else
        {
            iSum = iSum + (pcHead[iIndex] - 'A' + 10);
        }
    }
    cout<<"iSum : "<<iSum<<endl;

    int iTemp = 0;
    for (iIndex = 0; iSum != 0; iIndex++)
    {
        iTemp = iSum % iBase2;
        if(iTemp<=9 && iTemp>=0)
        {
            pcDest[iIndex] = iTemp + '0';
        }
        else
        {
            pcDest[iIndex] = iTemp - 10 + 'A';
        }
        iSum /= iBase2;
    }
    pcDest[iIndex] = '\0';
    vReverse(pcDest);
}

weixingongzhonghao

Similar Posts

Comments