简体   繁体   English

将大数的十进制转换为二进制

[英]Convert decimal to binary in big number

I want to create a big number with 128 bit.我想用 128 位创建一个大数字。

When I convert the string decimal to binary and set bit to the new data QInt :__int64 a[2] , it only true for a small number (about 10 digits).当我将字符串十进制转换为二进制并将位设置为新数据QInt :__int64 a[2] ,它仅true于小数(约 10 位)。

This is my code: http://codepad.org/HmYqMQme这是我的代码: http : //codepad.org/HmYqMQme

#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
//new data
class QInt
{
private:
    __int64 a[2];
public:
    void Get()
    {
        cout << a[0] << endl;
        cout << a[1] << endl;
    }
    QInt()
    {
        a[0] = 0;
        a[1] = 0;
    }
    //the funtion get the string binary of a[1]
    char* GetQInt(char *A);
    //the devide two of string decimal
    char* Div2(char *Str);
    //the funtion set bit to a[0] and a[1]
    void Setbit(int i, int bit);
    //con vert decimal to binary
    QInt ConvertDecimalToBinary(char *De, char *Bi);
};
//the funtion get the string binary of a[1]
char *QInt::GetQInt(char *A)
{

    for (int i = 0; i < 64; i++)
    {
        if (((a[1] >> i) & 1) != 0)
        {
            A[63 - i] = 49;

        }
        else
        {
            A[63 - i] = 48;
        }
    }
    return A;
}
// the funtion set bit to a[0] and a[1]
void QInt:: Setbit(int i, int bit)
{

    //if i<64 we set bit to a[1]
    if (i < 64)
    {
        if (bit==1)
        {
            a[1]=(1 << i) | a[1];

        }


    }//similar to a[1]
    else
    {
        if (bit == 1)


            a[0]=(1 << i) | a[0];

    }
}
//the devide two of string decimal
char*QInt:: Div2(char *Str)
{

    char Arr[100];
    int n = strlen(Str);
    int a = 0;//lay phan du
    int i = 0;
    int j = 0;
    while (Str[i] == 0)
    {
        i++;
    }


    for (i; i < n; i++)
    {
        int c = Str[i] - 48 + a * 10;
        a = c % 2;

        Arr[j] = c / 2 + 48;
        j++;
    }
    Arr[i] = '\0';

    for (i = 0; i < strlen(Arr); i++)
    {
        Str[i] = Arr[i];
    }
    Str[i] = '\0';
    return Str;
}
//con vert decimal to binary
QInt QInt::ConvertDecimalToBinary(char *De,char *Bi)

{
    int bit;
    int i = 127;
    int lenth = strlen(De);
    while (1)
    {
        //variable h use to count the number 0 of the string decimal,if h=lenth,exit
        int k = 0;
        int h = 0;
        while (De[k])
        {
            if (De[k] == '0')
                h++;
                k++;
        }
        if (h == lenth)
            break;
        else
        {

            bit = (De[lenth - 1] - 48) % 2;
            Bi[i] = bit + 48;
            Setbit(127 - i, bit);
            De = Div2(De);
            i--;
        }
    }
    Bi[128] = NULL;

    return *this;
}


int main()
{
    char s[100];
    char b[200];
    char c[200];
    for (int i = 0; i < 128; i++)
    {
        b[i] = 48;
    }
    cout << "Please enter a string : ";
    gets_s(s, 99);
    QInt a;

    a.ConvertDecimalToBinary(s, b);
    a.Get();
    a.GetQInt(c);
    for (int i = 0; i < 64; i++)
        cout << b[i];
    cout << endl;
    for (int i = 64; i < 128; i++)
        cout << b[i];
    cout << endl;
    for (int i = 0; i < 64; i++)
    {
        cout << c[i];
    }
    cout << endl;

    system("pause");

    return 0;
}

If i==100 , what do you think (1 << i) does?如果i==100 ,你认为(1 << i)是什么? What should it do?应该怎么做? And why are you ignoring a[0] in GetQInt ?为什么你在GetQInt忽略a[0]

In general, you have quite a few problems.一般来说,你有很多问题。 You're not comfortable with std::string , and making a mess of the char* everywhere.您对std::string感到不舒服,并且到处都弄乱了char* You're not clearly articulating (not even to yourself) what methods are supposed to be doing.你没有清楚地表达(甚至不是对你自己)应该做什么方法。 You're putting mathods in a class that are unrelated to the class.您将数学放在与课程无关的课程中。 Even worse, some of those methods redefine the name a !更糟糕的是,其中一些方法重新定义了名称a That is very confusing.这是非常令人困惑的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM